zmq-json-sub.js
================
A node.js module for subcribing to JSON messages via 0MQ (ZeroMQ)
-----------------------------------------------------------------
On a Mac:
$ brew install zmq
To verify the installation
$ man zmq
See __zeromq.org__
$ npm init
$ npm install zmq-json-sub --save
The complete example can be found in the __examples/demo__ folder.
This example shows how to create a simple app that subcribes to JSON messages from a publisher.
File: __examples/demo/config.js__
var config = {};
config.endpoint = "tcp://localhost:5431";
module.exports = config;
File: __examples/demo/index.js__
"use strict";
var ZmqJsonSub = require('zmq-json-sub'),
config = require('./config'),
sub = null;
var spec = {
endpoint: config.endpoint,
filter: "",
onMessage: function (data) {
let jstring = JSON.parse(data);
console.log( jstring );
if (jstring.timestamp) {
console.log( "TIME STAMP: ["
+ new Date(jstring.timestamp)
+ "]");
}
}
};
sub = new ZmqJsonSub(spec, function(err, data) {
if (err) {
console.log(err);
return;
}
});
console.log("Subscribed to: " + config.endpoint);
/*
Handle termination - close sub.
*/
// terminator === the termination handler.
function terminator(sig) {
if (typeof sig === "string") {
console.log('%s: Received %s - terminating Node server ...',
Date(Date.now()), sig);
console.log("Closing subcriber before exit");
sub.close();
process.exit(1);
}
console.log('%s: Node server stopped.', Date(Date.now()) );
}
// Process on exit and signals.
process.on('exit', function() { terminator(); });
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS',
'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGPIPE', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() { terminator(element); });
});
To run:
$ node index.js
To stop press __Ctrl-C__.
To test with some messages see the __example/demo__ project in the companion project https://www.npmjs.com/package/zmq-json-pub. Run the subscriber demo in one terminal window and the publisher demo in another. The subscriber should log to the console messages generated by the publisher.
Tests assume that __mocha__ has been installed globally. If not execute the following:
$ npm install -g mocha
Run the tests from the projects root folder in one of the following ways:
$ mocha --recursive --timeout 20000
Or
$ npm test
Or if you feel like kickin' it old skool:
make test
To run the tests for each version (currently there is only one version (v0001)):
$ mocha --timeout 5000 --recursive test/v0001/*test.js
The tests generate log files in a logs/ folder under the projects root folder.
*
* bitbucket.org/mitchallen/zmq-json-sub.git
*
In lieu of a formal style guide, take care to maintain the existing coding style.
Add unit tests for any new or changed functionality. Lint and test your code.
*
#### Version 0.1.1 release notes
* Updated demo to use live module
#### Version 0.1.0 release notes
* Initial release