publet

`npm install publet`

npm install publet
2 downloads in the last week
4 downloads in the last month

publet

npm install publet

Redis publish/subscribe with EventEmitter interface and efficient Buffer support.

Testing

To test, make sure you have a running redis-server.

  1. cd into node_modules/publet
  2. npm install ./
  3. make test

To benchmark Publet against zeromq, make bench. The benchmark compares sending 50000 strings and buffers from forked child processes using the zeromq.node module in push/pull mode and Publet in emitter/receiver mode.

Example

To operate a fully-fledged emitter/receiver, two Redis clients are required. Emitted events are queued until both the emitter and receiver client are connected and ready.

var publet = require('publet');
var emitter = publet();

emitter.on('data', function(data) {
  console.log('Data', data);
});

emitter.emit('data', 'Greetings');

publet.{emitter, receiver}

However, you might not always need an emitter/receiver. Sometimes either is fine. In this case we don't bother synchronizing, so the startup time should be marginally faster.

var cluster = require('cluster');
var cpus = require('os').cpus().length;
var publet = require('publet');

if (cluster.isMaster) {
  var receiver = publet.receiver();
  receiver.on('message', function(msg) {
    console.log(msg); // 'greetings'
  });
  while (cpus--) clsuter.fork();
} else {
  var emitter = publet.emitter();
  emitter.emit('message', 'greetings');
};

Buffers

Publet supports Buffers. And don't worry, it does not JSON.stringify an entire Node Buffer object. However, the buffer-to-arraylike-object conversion is marginally slower than plain other JSON.stringifiables.

var publet = require('publet');
var emitter = publet();

emitter.on('message', function(msg) {
  console.log(Buffer.isBuffer(msg), msg.toString()); // true 'test'
});

emitter.emit('message', new Buffer('test'));

EventEmitter API

Publet's constructor tries not to clobber your namespace too good. Publet also tries to preserve the EventEmitter API. This means you could extend a Publet EventEmitter much in the same way that you could extend a regular Node EventEmitter.

var util = require('util');
var publet = require('publet');

function Sender() { 
  publet.EventEmitter.apply(this);
};

util.inherits(Sender, publet.EventEmitter);

Sender.prototype.send = function() {
  this.emit.apply(this, arguments);
};

Sender.prototype.listen = function(event, callback) {
  this.addListener(event, callback);
};

var sender = new Sender;

sender.on('ready', function() {
  sender.listen('message', function(msg) {
    console.log(msg); // 'test'
  });
  sender.send('message', 'test');
});
npm loves you