node package manager

Build Status NPM version David DM


An express inspired, event-driven framework for building real-time distributed apps with and redis.

The Server

var express = require('express');
var app = express();
var server = require('http').Server(app).listen(3000);
var bus = require('')(server);

The Client

<script type="text/javascript" src="/"></script>
<script type="text/javascript">
  var client = io.connect();
  client.on('connect', function () {
    client.emit('echo', 'Hello, World!');
  client.on('echo', function (msg) {


  • An event-driven architecture provides scalability.
  • Socket events are encapsulated as Message objects.
  • Message objects are evenly distributed over all running app processes.
  • Standard interface for creating, handling, propagating, and consuming messages.
  • Sockets are associated to actors because messages are delivered to actors.
  • Express like routing and error handling.

How this works

Each socket is associated with one ore more actors. When a socket receives data, the data is encapsulated as a messsage and written to a queue. Since all of your app instances are connected to that queue, one of them will receive the message for processing. After the instance processes the message it can be delivered to the target. A target is just another actor, so if your actor is associated with multiple sockets. Each socket, regardless of which app instance it is connected to, will receive the data from the message.

Installation and Environment Setup

Install node.js (See download and install instructions here:

Install redis (See download and install instructions

For using

> npm install

For developing

Clone this repository.

> git clone

cd into the directory and install the dependencies

> cd
> npm install && npm shrinkwrap --dev



The Server connects the Messages instance to the `Exchange instance. It also provides ways to bind middleware functions to manipulate messages incomming from the client to the bus, messages processing on the bus, and finally messages outgoing from the bus to the client.

var Bus = require('');


The current version of the software.



The Server exposes it self.

var bus = require('').Server(3000);


The Exchange class queues and propagates messages to your Server. See for more information.

var Exchange = require('').Exchange;


The Messages class is the interface between the Server and the client. See for more information.

var Messages = require('').Messages;


var bus = require('')();


var bus = require('')(3000);


var io = require('')();
var bus = require('')(io);


var server = require('http').createServer(function (req, res) {}).listen(function (err) {});
var bus = require('')(server)


Sets the function that will grab the actor. The default implementation will use the This method is called when the socket connection is established. (sock, cb) {

The callback cb takes two parameters err and actor.

You may pass an Error object for the first argument if you encounter an error or would like to trigger one. (sock, cb) {
  sock.get('user', function (err, user) {
    if (err) return cb(err);
    if (!user) return cb(new Error('Need to login'));
    return cb(null,;


Gets the function that will grab the actor from a socket.

var actorFn =;


Sets the function that will grab the target from the request. The default implementation will use the This method is called for each request from the sock.

The client would emit this.

sock.emit('shout', 'hello', 'You');

We would like "You" to be the actor. (sock, params, cb) {
  cb(null, params.pop());

If you encounter an error you can also pass one along. (sock, params, cb) {
  if (params.length === 0) {
    cb(new Error('You are you talking to?!'));
  else {
    cb(null, params.pop());

You get to decide your own convention.


Gets the method that will grab the target from the request.

var targetFn =;


This method will allow you to bind a function to the connection event that supports.


We would like to tell the client "Hello" when they connect.

bus.socket(function (sock, bus) {
  sock.emit('greet', 'Hello');

Server#alias(sock:Object, name:String)

With alias your actor will receive messages whenever their alias receives one. This is useful if you want to associate a socket to a logged in user.

bus.alias(sock, 'nathan');

A good place to do this is when the client is connected to the server.

bus.socket(function (sock, bus) {
  sock.get('user', function (err, user) {
    if (err) return sock.emit('error', err);
    if (!user) return sock.emit('login', 'You must login');

Server#unalias(sock:Object, name:String)

With unalias your actor will no longer receive messages whenever their alias receives one. This is useful if you want to control messages going to your socket.

bus.unalias(sock, 'nathan');

An example is if you do not want to listen to messages from another user.'stop following', function (msg, sock, next) {


The in method will use the passed function(s) when a message is received from the sock. This allows you to modify the message before it is sent to the exchange. (msg, sock, next) {

You can pass in multiple functions or arrays of functions. (a,b,c) {...}, function (a,b,c) {...}, [function (a,b,c) {...}, function(a,b,c) {...}]);

You can set up handlers for specific messages.'chat', function (msg, sock, next) {
  // do something 

If you bind multiple handlers they will be called in this order'chat', function (msg, sock, next) {
}); (msg, sock, next) {
});'chat', function (msg, sock, next) {
}); (err, msg, sock, next) {
// The output of msg.content() will be 'ABC'; 

You can control propagation with consume(), deliver(), respond(), errored() as well. (msg, sock, next) {
}); (msg, sock, next) {
  // will not be called because the message will delivered to the target as a result of calling deliver!! 
}); (msg, sock, next) {
  // the message will just die here 

Server#on(event:String, fn:Function)

The on method binds a handler to the queue. The handler will process each message and give you the ability to either deliver the message or discard it. That is up to your application requirements.

bus.on('some event', function (msg) {

Or you can use the optional next parameter. You may eiter call next() to invoke the next handler. Or you may call msg.deliver(), msg.respond(), or msg.consumed() to control the message's propagation.

bus.on('some*', function (msg, next) {
  // do something! 


The out method will use the passed function(s) when a message is received from the exchange. This allows you to modify the message before it is sent to the sock.

Here you could save the message to a mongo store using mongoose.

//assuming you have mongoose and a message model 
var Message = monngose.model('Message');
bus.out(function (msg, sock, next) {
  new Message( (err) {
    if (err) return next(err);

You can pass in multiple functions or arrays of functions.

bus.out(function (a,b,c) {...}, function (a,b,c) {...}, [function (a,b,c) {...}, function(a,b,c) {...}]);

You can set up handlers for specific messages.

bus.out('chat', function (msg, sock, next) {
  // do something 

If you bind multiple handlers they will be called in this order

bus.out('chat', function (msg, sock, next) {
bus.out(function (msg, sock, next) {
bus.out('chat', function (msg, sock, next) {
bus.out(function (err, msg, sock, next) {
assert.equal(msg.content(), 'ABC');

You can control propagation with consume(), deliver(), respond(), errored() as well.

bus.out(function (msg, sock, next) {
bus.out(function (msg, sock, next) {
  // will not be called because the message will delivered to the target as a result of calling deliver!! 
bus.out(function (msg, sock, next) {
  // the message will just die here 


You can either pass a port, server, or instance.

bus.listen(require('http').createServer(function (req, res) { }));


This method is a convenient way to deliver a message.

bus.deliver({actor:'I', action:'say', content:'hello', 'you'});


This is an alias to message().


This will create you an object for building a message that you can deliver. The data can either be an object or an instanceof of Message.


A chain-able approach.


Simply put.



Gets the exchange the server uses to publish information.


var exchange =;


Sets the exchange the server uses to publish information.


for more information.

var exchange = require('').Exchange();;


Gets the Queue the Exchange uses.

var queue = exchange.queue();


Sets the Queue the Exchange uses.


var queue = require('').Exchange.Queue();


Gets the PubSub the Exchange uses.


var pubsub = exchange.pubsub();


Sets the PubSub the Exchange uses.

var pubsub = require('').Exchange.PubSub();


Instead of having to write a function to deliver a message like this.

bus.on(/some message/, function (msg) {

We could call autoPropagate(true) so that any method we have not declared a handler for will automatically be propagated.


Auto-propagation is on by default. You may turn it off to prevent unwanted messages from going into your bus.


This method will pass the bus instance into your function. Your function can then do whatever it needs to attach it self to your bus.

In this example we create a middleware that will emit an event whenever we handle a message going in(), on(), or out() of the bus.

function tracker (emitter) {
  return function (bus) {
    function handler (event) {
      return function (msg, next) {
        emitter.emit('track', event, msg);
var receiver = new require('events').EventEmitter();
report.on('track', function (point, msg) { = || {};[point] =[point] || {};[point][msg.action()] =[point][msg.action()] || 0;[point][msg.action()] += 1;
report.on('report', function () {
setInterval(function () {
}, 1000);

Core Components is broken down into other components.

  • contains all the common code such as the Message, Builder, and Controller classes.
  • contains all the code that will be used to handle messages going into the Queue and propagation on the PubSub.
  • contains the code that handles listening to a Socket for an event and building that into a message.
  • contains the code that handles receiving messages in, on, and out the bus.
  • is a wrapper for that provides the interface on the client.


Test drive your apps with

  • The driver helps you test driver in your apps.


Middleware components you can use in your apps.

  • The monitor helps your apps report on the messages being processed.
  • The session is used to maintain state for socket connections and multiple servers.

Running Tests

Install coffee-script

> npm install coffee-script -g

Tests are run using grunt. You must first globally install the grunt-cli with npm.

> sudo npm install -g grunt-cli

Unit Tests

To run the tests, just run grunt

> grunt spec

Working Examples and Demos

You will need a redis server up and running to run the demos at this time.


Check out the (README)( "examples") for the examples

> node examples/echo.js


Demos are under the /demo directory. There is currently a basic chat program.


There are open issues if you would like to contribute please fork and send me a pull request!