An auto-discovery mesh network framework for building fault-tolerant and scalable applications
cote is an auto-discovery mesh network framework for building fault-tolerant and scalable applications. It hosts components that let you write programs that discover each other over LAN and communicate in various schemes.
Tomorrow belongs to distributed software. As CPU performance is heavily dictated by the number of cores and the power of each core is already at its limits, distributed computing will decide how your application performs. Distributed systems also pose great architectural benefits such as fault-tolerance and scalability.
Components of such a distributed system should be able to find other components zeroconf and communicate over a set of conventions. Sometimes they may work as a cluster, may include a pub/sub mechanism, or a request/response mechanism. Cote brings you the advantages of distributed software. Think of it like homing pigeons.
npm install cote
git clone git://github.com/dashersw/cote.git
All components support namespaces. Given as the configuration object to the constructor, components adhere and act on namespaces if provided, and ignore other messages.
Requester queues requests until a Responder is available, and once so, it delivers the request. Requests will be dispatched to Responders in a round-robin way.
var Requester = require'cote'Requester;var randomRequest =name: 'randomReq'// namespace: 'rnd',requests: 'randomRequest';randomRequeston'ready'setIntervalvar req =type: 'randomRequest'val: ~~Mathrandom * 10;randomRequestsendreqconsole.log'request' req 'answer' res;;5000;;
Responder is a component for responding to certain requests from a Requester. It's a descendant of EventEmitter2, and requests are regular events, therefore may be wildcarded or namespaced.
Responder may be used to add new modules to existing web servers / applications without ever changing the main server code. Only a Requester will be able to utilize a Responder.
var Responder = require'cote'Responder;// Instantiate a new Responder component.var randomResponder =name: 'randomRep'// namespace: 'rnd',respondsTo: 'randomRequest' // types of requests this responder// can respond to.;// request handlers are like any event handler.randomResponderon'randomRequest'var answer = ~~Mathrandom * 10;console.log'request' reqval 'answering with' answer;cbanswer;;
Publisher is a component for publishing certain events with arbitrary data. It may be used as a distributed EventEmitter. It may also be used in a scenario where some components need to be notified of updates, such as new tweets, etc. instead of polling for them. Only a subscriber will get notifications from a Publisher.
var Publisher = require'cote'Publisher;// Instantiate a new Publisher component.var randomPublisher =name: 'randomPub'// namespace: 'rnd',broadcasts: 'randomUpdate';// Wait for the publisher to find an open port and listen on it.randomPublisheron'ready'setIntervalvar val =val: ~~Mathrandom * 1000;console.log'emitting' val;// publish an event with arbitrary data at any timerandomPublisherpublish'randomUpdate' val;3000;;
Subscriber subscribes to events emitted from a Publisher.
var Subscriber = require'cote'Subscriber;var randomSubscriber =name: 'randomSub'// namespace: 'rnd',subscribesTo: 'randomUpdate';randomSubscriberon'randomUpdate'console.log'notified of ' req;;
Sockend is the glue for carrying all the possibilities of cote to the next level with WebSockets over socket.io. Sockend makes Responders and Publishers available to the front-end and adhere to socket.io namespaces. It's the magic for distributed web apps.
var app = require'http'createServerhandlerio = require'socket.io'listenappfs = require'fs'applisten5555;fsreadFile__dirname + '/index.html'if errreswriteHead500;return resend'Error loading index.html';reswriteHead200;resenddata;;;var sockend = '../../'Sockendioname: 'sockend';
Now, fire up a few Responders and Publishers on default or 'rnd' namespace and watch them glow with magic on
Monitor is the "top" of cote. It lists all the daemons it discovers regardless of namespace or key. Run
examples/monitor.js and see all your active cote daemons.
Copyright (c) 2013 Armagan Amcalar email@example.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.