1.0.10 • Published 9 years ago

redis-messenger v1.0.10

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

redis-messenger

This is a simple library that uses Redis pub/sub to provide intercommunication for node apps. It doesn't setup any sort of master/slave relationship model.

Install

npm install redis-messenger

Api

create(port, host, options)

Create a new redis messenger object. The defaults are

{
  "port":6379,
  "host":"127.0.0.1",
  "options":{}
}
var messenger = require('redis-messenger').create()

register(appName , callback)

Registering sets your messenger up with a pseudo-unique channel name that it can receive messages on. To retrieve your name, see the whoAmI command.

messenger.register('myAppName');

The callback receives 1 parameter that is the resulting application name that was used for registration. It may not be what you registered as, since that channel may be taken by another application already.

Why not register using the create command? Registering is an asynchronous function which require() cannot do.

send(to, eventName, content)

Send a message to a channel

messenger.send('someApp', 'moshimoshi', { foo: 'bar' });

The send command will wait until your application has successfully registered with a name. This is because the receiver needs an application name to respond to. Once you have registered, the messages will automatically be sent.

Note: It is possible to send to channels that have no listeners. The message will just be thrown away as this is how Redis pub/sub works.

isAlive(id, callback)

Checks if a channel is open or not. The callback receives a single boolean parameter

messenger.isAlive('someChannel', function(aliveness){ ... });

join(channel)

Joins a channel to listen to events

messenger.join('someChannel');

leave(channel)

Leaves a channel that was subscribed to

messenger.leave('someChannel');

unregister()

Leaves the channel that the application was registered to.

messenger.unregister('someChannel');

Warning: This removes the unique channel joining feature.

Handle Messages

The messenger is simply an EventEmitter, so all events are handled accordingly.

For each message, the callback receives the content, sender address, and channel.

messenger.on('someEventName', function(data, sender, channel){
  console.log(sender+"sent a message over channel "+channel+":", data);
});

Example

fooApp.js

var messenger = require('redis-messenger').create();
// register to a specific channel
messenger.register('fooApp');

messenger.on('ping', function(data, sender){
   console.log(sender, ': ping');
   console.log('fooApp : pong');
   messenger.send(sender, 'pong');
});

barApp.js

var messenger = require('redis-messenger').create();
// register to a specific channel
messenger.register('barApp');

messenger.on('pong', function(data, sender){
   console.log(sender, ': pong');
});

setInterval(function(){
   console.log("barApp : ping");
   messenger.send('fooApp', 'ping');
}, 5000);

License

Copyright (c) 2015 Brett Berry

Released under the MIT license.

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago