particle-iot/zeromq.node

Name: zeromq.node

Owner: Particle

Description: ? DEPRECATED Use zeromq.js - Node.js bindings to the zeromq library

Forked from: JustinTulloss/zeromq.node

Created: 2016-10-31 19:56:46.0

Updated: 2018-03-10 14:36:12.0

Pushed: 2018-01-03 18:51:16.0

Homepage:

Size: 2296

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

zmq   Build Status Build status

ØMQ bindings for node.js.

Installation
on Windows:

First install Visual Studio and either Node.js or io.js.

Ensure you're building zmq from a conservative location on disk, one without unusual characters or spaces, for example somewhere like: C:\sources\myproject.

Installing the ZeroMQ library is optional and not required on Windows. We recommend running npm install and node executable commands from a github for windows shell or similar environment.

installing on Unix/POSIX (and osx):

First install pkg-config and the ZeroMQ library.

This module is compatible with ZeroMQ versions 2, 3 and 4. The installation process varies by platform, but headers are mandatory. Most Linux distributions provide these headers with -devel packages like zeromq-devel or zeromq3-devel. Homebrew for OS X provides versions 4 and 3 with packages zeromq and zeromq3, respectively. A Chris Lea PPA is available for Debian-like users who want a version newer than currently provided by their distribution. Windows is supported but not actively maintained.

Note: For zap support with versions >=4 you need to have libzmq built and linked against libsodium. Check the Travis configuration for a list of what is tested and therefore known to work.

with your platform-specifics taken care of, install and use this module:
$ npm install zmq
Examples
Push/Pull
roducer.js
zmq = require('zmq')
sock = zmq.socket('push');

.bindSync('tcp://127.0.0.1:3000');
ole.log('Producer bound to port 3000');

nterval(function(){
nsole.log('sending work');
ck.send('some work');
00);
s
orker.js
zmq = require('zmq')
sock = zmq.socket('pull');

.connect('tcp://127.0.0.1:3000');
ole.log('Worker connected to port 3000');

.on('message', function(msg){
nsole.log('work: %s', msg.toString());

Pub/Sub
ubber.js
zmq = require('zmq')
sock = zmq.socket('pub');

.bindSync('tcp://127.0.0.1:3000');
ole.log('Publisher bound to port 3000');

nterval(function(){
nsole.log('sending a multipart message envelope');
ck.send(['kitty cats', 'meow!']);
00);
s
ubber.js
zmq = require('zmq')
sock = zmq.socket('sub');

.connect('tcp://127.0.0.1:3000');
.subscribe('kitty cats');
ole.log('Subscriber connected to port 3000');

.on('message', function(topic, message) {
nsole.log('received a message related to:', topic, 'containing message:', message);

Monitoring

You can get socket state changes events by calling to the monitor function. The supported events are (see ZMQ docs for full description):

All events get 2 arguments:

A special monitor_error event will be raised when there was an error in the monitoring process, after this event no more monitoring events will be sent, you can try and call monitor again to restart the monitoring process.

monitor(interval, numOfEvents)

Will create an inproc PAIR socket where zmq will publish socket state changes events, the events from this socket will be read every interval (defaults to 10ms). By default only 1 message will be read every interval, this can be configured by using the numOfEvents parameter, where passing 0 will read all available messages per interval.

unmonitor()

Stop the monitoring process

example
reate a socket
zmq = require('zmq');
et = zmq.socket('req');

egister to monitoring events
et.on('connect', function(fd, ep) {console.log('connect, endpoint:', ep);});
et.on('connect_delay', function(fd, ep) {console.log('connect_delay, endpoint:', ep);});
et.on('connect_retry', function(fd, ep) {console.log('connect_retry, endpoint:', ep);});
et.on('listen', function(fd, ep) {console.log('listen, endpoint:', ep);});
et.on('bind_error', function(fd, ep) {console.log('bind_error, endpoint:', ep);});
et.on('accept', function(fd, ep) {console.log('accept, endpoint:', ep);});
et.on('accept_error', function(fd, ep) {console.log('accept_error, endpoint:', ep);});
et.on('close', function(fd, ep) {console.log('close, endpoint:', ep);});
et.on('close_error', function(fd, ep) {console.log('close_error, endpoint:', ep);});
et.on('disconnect', function(fd, ep) {console.log('disconnect, endpoint:', ep);});

andle monitor error
et.on('monitor_error', function(err) {
console.log('Error in monitoring: %s, will restart monitoring in 5 seconds', err);
setTimeout(function() { socket.monitor(500, 0); }, 5000);


all monitor, check for events every 500ms and get all available events.
ole.log('Start monitoring...');
et.monitor(500, 0);
et.connect('tcp://127.0.0.1:1234');

imeout(function() {
console.log('Stop the monitoring...');
socket.unmonitor();
0000);
Detaching from the event loop

You may temporarily disable polling on a specific ZMQ socket and let the node.js process to terminate without closing sockets explicitly by removing their event loop references. Newly created sockets are already ref()-ed.

unref()

Detach the socket from the main event loop of the node.js runtime. Calling this on already detached sockets is a no-op.

ref()

Attach the socket to the main event loop. Calling this on already attached sockets is a no-op.

Example
zmq = require('zmq');
et = zmq.socket('sub');
et.bindSync('tcp://127.0.0.1:1234');
et.subscribe('');
et.on('message', function(msg) { console.log(msg.toString(); });
ere blocks indefinitely unless interrupted.
et it terminate after 1 second.
imeout(function() { socket.unref(); }, 1000);
Running tests
Install dev deps:
t clone https://github.com/JustinTulloss/zeromq.node.git zmq && cd zmq
m i
Build:
 unix:
ke

ilding on windows:
m i
Test:
 unix:
ke test

sting on windows:
m t
Running benchmarks

Benchmarks are available in the perf directory, and have been implemented according to the zmq documentation: How to run performance tests

In the following examples, the arguments are respectively:

You can run a latency benchmark by running these two commands in two separate shells:

 ./local_lat.js tcp://127.0.0.1:5555 1 100000
h
 ./remote_lat.js tcp://127.0.0.1:5555 1 100000

And you can run throughput tests by running these two commands in two separate shells:

 ./local_thr.js tcp://127.0.0.1:5555 1 100000
h
 ./remote_thr.js tcp://127.0.0.1:5555 1 100000

Running make perf will run the commands listed above.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.