Skip to content

MartinKnopf/zmq-toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zmq-toolkit Build Status

Some helpers for simplified use of zeromq.node.

Installation

Look at the supported Node and zmq versions here.

$ npm install zmq-toolkit

To start the broker in its own process:

$ npm install -g zmq-toolkit
$ zmqbroker tcp://127.0.0.1:11111 tcp://127.0.0.1:22222

API

Broker

// pubsub proxy that binds to the given XSUB and XPUB sockets

var Broker = require('zmq-toolkit').Broker
  , broker = new Broker()
    .start('tcp://127.0.0.1:11111', 'tcp://127.0.0.1:22222');

ZmqEventEmitter

// zeromq based EventEmitter that connects to the zeromq xpub/xsub broker

var ZmqEventEmitter = require('zmq-toolkit').ZmqEventEmitter
  , zee = new ZmqEventEmitter()
    .start('tcp://127.0.0.1:11111', 'tcp://127.0.0.1:22222')
    .on('my-event', function(options) {
      console.log(options.foo);
    })
    .on('my-other-event', function(options) {
      console.log(options.foo);
    });

setTimeout(function() { // need some time to connect sockets
  zee.emit('my-event', {foo: 'bar'});
}, 100);

Connecting socket.io sockets to the zmq broker

// connect socket.io sockets to a zeromq xpub/xsub broker

var ZmqEventEmitter = require('zmq-toolkit').ZmqEventEmitter
  , zee = new ZmqEventEmitter()
    .start('tcp://127.0.0.1:11111', 'tcp://127.0.0.1:22222');

io.sockets.on('connection', function (socket) {
  zee.hug(socket);

  socket.emit('foo'); // will be forwared to zee

  socket.on('bar', function() {
    console.log('baz');
  });
  zee.emit('bar'); // will invoke the websocket's handler for this event
});

Heartbeat publisher

// periodically emit a ```heartbeat``` event with arbitrary data to a zeromq xpub/xsub broker

var Heartbeat = require('zmq-toolkit').Heartbeat
  , heartbeat = new Heartbeat({name: 'my-app'})
    .start('tcp://127.0.0.1:11111', 60000); // connet to xsub socket, emit every 60 seconds

About

Helpers for simplified use of zeromq.node

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published