Integrates Redis as a queue and a pubsub by providing an easy mechanism for publishing and handling messages
A bus.io dependency.
The message exchange provides an iterface for publishing a message to a queue, handling that message, and potentially propagating that message to its destination.
Install node.js (See download and install instructions here: http://nodejs.org/).
Install redis (See download and install instructions http://redis.io/topics/quickstart)
> npm install coffee-script -g
Clone this repository
> git clone firstname.lastname@example.org:turbonetix/bus.io-exchange.git
cd into the directory and install the dependencies
> cd bus.io-exchange > npm install && npm shrinkwrap --dev
This is wheere we publish, handle, and propagate messages.
var exchange = require'bus.io-exchange';
var Exchange = require'bus.io-exchange';var exchange = ExchangeExchangeQueue ExchangePubSub;
var Exchange = require'bus.io-exchange';var queue = ExchangeQueue;var pubsub = ExchangePubSub;var handler = ;var exchange = Exchangequeue pubsub handler;
Puts the message onto the
Queue if the message has not already been published to the
If the message has already been published to the
Queue it will be published onto the
var Message = require'bus.io-common'Message;exchangepublish Message ;
Puts the message onto the
PubSub with the
var message = Message;exchangepublish message 'everyone' ;
listener to the channel and invokes the callback when the
channel as been subscribed.
exchangesubscribe'some channel'//this gets called when we receive a message on the channel//this gets called when we subscribed to the channel;
listener from the channel and invokes the callback when the
listener as been unsubscribed.
var ;exchangeunsubscribe'some channel' listener//this gets called when we unsubscribed from the channel;
var queue = Exchangequeue;queuesendMessage;
var kue = require'kue';var queue = messageExchangeQueuemakekuecreateClient;exchangequeuequeue;
Gets the pubsub instance.
var pubsub = exchangepubsub;pubsubsendmessage 'everyone';
Sets the pubsub instance.
var redis = require'redis';var pub = rediscreateClient;var sub = rediscreateClient;var pubsub = messageExchangePubSubmakepub sub;exchangepubsubpubsub;
Gets the handler which is an
var handler = exchangehandler;handleron'some message'// do somethingexchangechannelmessagetargetpublishmessage;;
Sets the handler.
var events = require'events';var handler = ;handleron'some message'// do somethingexchangechannelmessagetargetpublishmessage;;exchangehandlerhandler;
The queue is a lightweight wrapper around an object that supports a
process(name, fn). Where
name is a
fn is a
Function. It must also support the method
create(name, data) where
name is a
data is an
Object. The return value of
create method must expose a function
done(). In our case
we used the
Kue library. It is a really nice library for handling jobs.
var queue = ExchangeQueue;
var kue = require'kue';var queue = ExchangeQueuekuecreateQueue;
The pubusb is a lightweight wrapper around the
redis module. You could
pass in another object insead of the
redis object. By making sure it
supports these methods
var pubsub = ExchangePubSub;
var pub = rediscreateClientsub = rediscreateClient;var pubsub = ExchangePubSubpub sub;
var message = Message;pubsubsendmessage messagetarget;
pubsubsubscribe'channel'if err throw err;console.log'channel subscribed';;
pubsubunsubscribe'channel'if err throw err;console.log'channel unsubscribed';;
Tests are run using grunt. You must first globally install the grunt-cli with npm.
> sudo npm install -g grunt-cli
To run the tests, just run grunt
- Support different queues
- Support different pubsubs