integrates redis as a queue and a pubsub by providing an easy mechanism for publishing and handling messages
The goal of this library is to wrap-up kue and [redis] (https://www.npmjs.org/package/redis "Redis") and provide a simple interface for building event driven applications.
At a high-level the library will read messages off of a queue, attempt to handle them, and in some cases publish those events back out onto the pubsub (redis).
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:NathanGRomano/message-exchange.git
cd into the directory and install the dependencies
> cd message-exchange > npm install && npm shrinkwrap --dev
Here is how to create a simple application to handle an event and broadcast it back out.
First we get an exchage instance
var events = require'events';var exchange = require'message-exchange'make;
Lets initialize a model and setup a handler for a quit event
var employeeModel = hired: :var handler =handleron'quit'// handle the eventemployeeModelquit = eventcreated;// let Human Resources know the employee quitexchangechannel'human resources'publishevent;;exchangehandlerhandler;
When we handle a quit event we broadcast the messaage to the human resources channel. So let add a listener on that channel so we can relay the information to say a socket.
//presume we have declared sockets elsewhereexchangechannel'human resources'on'message'socketsemit'message' event;;
Now lets publish a quit event the message queue. It will be handled by our handler and then broadcast on our channel and finally broadcast to our sockets.
You can put anything you like into an event. I just like to follow a convention similar to what you saw. Make sure you have the required field "action" in your event.
This is wheere we publish, handle, and propagate messages.
var exchange = require'message-exchange'make;
var messageExchange = require'message-exchange';var queue = messageExchangeQueuemake;var pubsub = messageExchangePubSubmake;var handler = ;var exchange = messageExchangemakequeue pubsub handler;
Puts the message onto the
var message =actor: 'Me'action: 'shout'content: 'Hello'target: 'You';exchangepublish message ;
Puts the message onto the
PubSub with the
var message =actor: 'Me'action: 'shout'content: 'Hello'target: 'You';exchangepublish message 'everyone' ;
Gets a channel instance, if it doesn't already exist it will subscribe to that channel.
var channel = exchangechannel'everyone';channelon'message'//do somethign;
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;
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