Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

zmq

JustinTulloss11.5kMIT2.15.3TypeScript support: definitely-typed

Bindings for node.js and io.js to ZeroMQ

zeromq, zmq, 0mq, ømq, libzmq, native, binding, addon

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

// producer.js
var zmq = require('zmq')
  , sock = zmq.socket('push');

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

setInterval(function(){
  console.log('sending work');
  sock.send('some work');
}, 500);
// worker.js
var zmq = require('zmq')
  , sock = zmq.socket('pull');

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

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

Pub/Sub

// pubber.js
var zmq = require('zmq')
  , sock = zmq.socket('pub');

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

setInterval(function(){
  console.log('sending a multipart message envelope');
  sock.send(['kitty cats', 'meow!']);
}, 500);
// subber.js
var zmq = require('zmq')
  , sock = zmq.socket('sub');

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

sock.on('message', function(topic, message) {
  console.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):

  • connect - ZMQ_EVENT_CONNECTED
  • connect_delay - ZMQ_EVENT_CONNECT_DELAYED
  • connect_retry - ZMQ_EVENT_CONNECT_RETRIED
  • listen - ZMQ_EVENT_LISTENING
  • bind_error - ZMQ_EVENT_BIND_FAILED
  • accept - ZMQ_EVENT_ACCEPTED
  • accept_error - ZMQ_EVENT_ACCEPT_FAILED
  • close - ZMQ_EVENT_CLOSED
  • close_error - ZMQ_EVENT_CLOSE_FAILED
  • disconnect - ZMQ_EVENT_DISCONNECTED

All events get 2 arguments:

  • fd - The file descriptor of the underlying socket (if available)
  • endpoint - The underlying socket endpoint

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

// Create a socket
var zmq = require('zmq');
socket = zmq.socket('req');

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

// Handle monitor error
socket.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);
});

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

setTimeout(function() {
    console.log('Stop the monitoring...');
    socket.unmonitor();
}, 20000);

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

var zmq = require('zmq');
socket = zmq.socket('sub');
socket.bindSync('tcp://127.0.0.1:1234');
socket.subscribe('');
socket.on('message', function(msg) { console.log(msg.toString(); });
// Here blocks indefinitely unless interrupted.
// Let it terminate after 1 second.
setTimeout(function() { socket.unref(); }, 1000);

Running tests

Install dev deps:

$ git clone https://github.com/JustinTulloss/zeromq.node.git zmq && cd zmq
$ npm i

Build:

# on unix:
$ make

# building on windows:
> npm i

Test:

# on unix:
$ make test

# testing on windows:
> npm 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:

  • the host to connect to/bind on
  • message size (in bytes)
  • message count

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

node ./local_lat.js tcp://127.0.0.1:5555 1 100000
node ./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:

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

Running make perf will run the commands listed above.

changelog

2.15.3 / 2016-6-3

  • 2.15.0 introduced a bug where request sockets could no longer batch up requests. This release should fix that [ronkorving, jaleigh]

2.15.2 / 2016-5-22

  • 2.15.0 introduced a bug where some messages would not be received. This release should fix that [ronkorving]

2.15.1 / 2016-5-8

  • Node.js 6 compatibility (NAN 2.3) [kkoopa]

2.15.0 / 2016-4-27

  • Dropped support for Node 0.8 [reqshark]
  • Added unref/ref APIs to detach/attach sockets from/to the event loop [Joongi Kim]
  • Improved message throughput 3-fold on ZMQ 4 [ronkorving]
  • When bind or unbind failed, you could never try again (fixed) [ronkorving]
  • Various travis configuration improvements [reqshark]
  • Bumped NAN to 2.2.x [JanStevens]

2.14.0 / 2015-11-20

  • A socket.read() method was added to retrieve messages while paused [sshutovskyi]
  • socket.send() now takes a callback as 3rd argument which is called once the message is sent [ronkorving]
  • Now tested on Node.js 0.8, 0.10, 0.12, 4 and 5 [ronkorving]

2.13.0 / 2015-08-26

  • io.js 3.x compatible [kkoopa]
  • corrections to type casting operations [kkoopa]
  • "make clean" now also removes node_modules [reqshark]

2.12.0 / 2015-07-10

  • Massive improvements to monitoring code, with new documentation and tests [ValYouW]
  • Improved documentation [reqshark]
  • Updated bindings from ~1.1.1 to ~1.2.1 [reqshark]
  • Test suite improvements [reqshark]
  • Updated the Windows bundle to ZeroMQ 4.0.4 [kkoopa]
  • License attribute added to package.json [pdehaan]

2.11.1 / 2015-05-21

  • io.js 2.x compatible [transcranial]
  • replaced asserts with proper exceptions [reqshark]

2.11.0 / 2015-03-31

  • Added pause() and resume() APIs on sockets to allow backpressure [philip1986]
  • Elegant handling of EINTR return codes [hurricaneLTG]
  • Small performance improvements in send() and internal flush methods [ronkorving]
  • Updated test suite to cover io.js and Node 0.12 (removed 0.11) [ronkorving]
  • Added "make perf" for easy benchmarking [ronkorving]

2.10.0 / 2015-01-22

  • Added ZMQ_STREAM socket type [reqshark]
  • Update NAN to io.js compatible 1.5.0 [kkoopa]
  • Hitting open file descriptor limit now throws an error during zmq.socket() [briansorahan]
  • More reliable benchmarking [maxired]

2.9.0 / 2015-01-05

  • More unit tests [bluebery and reqshark]
  • More reliable testing [f34rdotcom and kkoopa]
  • Improved ReadMe [dminkovsky and skibz]
  • Support for zmq_proxy sockets [reqshark]
  • Removed "docs" and related deps in favor of ReadMe [reqshark]

2.8.0 / 2014-08-27

  • Fixed: monitor API would keep CPU busy at 100% [f34rdotcom]
  • Fixed: an exception during flush could render a socket unusable [ronkorving]
  • Fixed: Travis changed behavior and broke our tests [ronkorving]
  • Code cleanup [kkoopa and ronkorving]
  • Removed legacy nextTick event emission during flush [utvara and ronkorving]
  • Context API added: setMaxThreads, getMaxThreads, setMaxSockets, getMaxSockets [yoneal]
  • Changed unit test suite to Mocha [skeggse and yoneal]
  • NAN updated to ~1.3.0 [kkoopa]

2.7.0 / 2014-04-24

  • Fixed memory leak when closing socket [rasky]
  • Fixed high water mark [soplwang, kkoopa]
  • Added socket opts for zeromq 4.x security mechanisms [msealand]
  • Use MakeCallback [kkoopa]
  • Remove useless setImmediate [kkoopa]
  • Use zmq_msg_send for ZMQ >= 4.0 [kkoopa]
  • Expose the Socket class as zmq.Socket [tcr]

2.6.0 / 2014-01-23

  • Monitor support [f34rdotcom, dr-fozzy]
  • Unbind support [kkoopa]
  • Node 0.11.9 compatibility [kkoopa]
  • Support for ZMQ 4 [atrniv]
  • Fixed memory leak [utvara]
  • OSX Homebrew support [jwalton]
  • Fix unit tests [ryanlelek]

2.5.1 / 2013-08-28

  • Regression fix for IPC socket bind failure [christopherobin]

2.5.0 / 2013-08-20

  • Added testing against Node.js v0.11 [AlexeyKupershtokh]
  • Add support for Joyent SmartMachines [JonGretar]
  • Use pkg-config on OS X too [blalor]
  • Patch for Node 0.11.3 [kkoopa]
  • Fix for bind / connect / send problem [kkoopa]
  • Fixed multiple bugs in perf tests and changed them to push/pull [ronkorving]
  • Add definitions for building on openbsd & freebsd [Minjung]

2.4.0 / 2013-04-09

  • added: Windows support [mscdex]
  • added: support for all options ever [AlexeyKupershtokh]
  • fixed: prevent zeromq sockets from being destroyed by GC [AlexeyKupershtokh]

2.3.0 / 2013-03-15

  • added: xpub/xsub socket types [xla]
  • added: support for zmq_disconnect [matehat]
  • added: LAST_ENDPOINT socket option [ronkorving]
  • added: local/remote_lat local/remote_thr perf test [wavded]
  • fixed: tests improved [qubyte, jeremybarnes, ronkorving]
  • fixed: Node v0.9.4+ compatibility [mscdex]
  • fixed: SNDHWM and RCVHWM options were given the wrong type [freehaha]
  • removed: waf support [mscdex]

2.2.0 / 2012-10-17

  • add support for pkg-config
  • add libzmq 3.x support [aaudis]
  • fix: prevent GC happening too soon for connect/bindSync

2.1.0 / 2012-06-29

  • fix require() for 0.8.0
  • change: use uv_poll in place of IOWatcher
  • remove stupid engines field

2.0.3 / 2012-03-14

  • Removed -Wall (libuv unused vars caused the build to fail...)

2.0.2 / 2012-02-16

  • Added back .createSocket() for BC. Closes #86

2.0.1 / 2012-01-26

  • Added .zmqVersion [patricklucas]
  • Fixed multipart support [joshrtay]