Bindings for node.js and io.js to ZeroMQ
ØMQ bindings for node.js.
Ensure you're building zmq from a conservative location on disk, one without
unusual characters or spaces, for example somewhere like:
Installing the ZeroMQ library is optional and not required on Windows. We
npm install and node executable commands from a
github for windows shell or similar environment.
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
zeromq3-devel. Homebrew for OS X provides versions 4 and 3 with packages
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
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.
$ npm install zmq
// producer.jsvar zmq = require'zmq'sock = zmqsocket'push';sockbindSync'tcp://127.0.0.1:3000';console.log'Producer bound to port 3000';setIntervalconsole.log'sending work';socksend'some work';500;
// worker.jsvar zmq = require'zmq'sock = zmqsocket'pull';sockconnect'tcp://127.0.0.1:3000';console.log'Worker connected to port 3000';sockon'message'console.log'work: %s' msgtoString;;
// pubber.jsvar zmq = require'zmq'sock = zmqsocket'pub';sockbindSync'tcp://127.0.0.1:3000';console.log'Publisher bound to port 3000';setIntervalconsole.log'sending a multipart message envelope';socksend'kitty cats' 'meow!';500;
// subber.jsvar zmq = require'zmq'sock = zmqsocket'sub';sockconnect'tcp://127.0.0.1:3000';socksubscribe'kitty cats';console.log'Subscriber connected to port 3000';sockon'message'console.log'received a message related to:' topic 'containing message:' message;;
You can get socket state changes events by calling to the
The supported events are (see ZMQ docs for full description):
All events get 2 arguments:
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.
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
where passing 0 will read all available messages per interval.
Stop the monitoring process
// Create a socketvar zmq = require'zmq';socket = zmqsocket'req';// Register to monitoring eventssocketon'connect' console.log'connect, endpoint:' ep;;socketon'connect_delay' console.log'connect_delay, endpoint:' ep;;socketon'connect_retry' console.log'connect_retry, endpoint:' ep;;socketon'listen' console.log'listen, endpoint:' ep;;socketon'bind_error' console.log'bind_error, endpoint:' ep;;socketon'accept' console.log'accept, endpoint:' ep;;socketon'accept_error' console.log'accept_error, endpoint:' ep;;socketon'close' console.log'close, endpoint:' ep;;socketon'close_error' console.log'close_error, endpoint:' ep;;socketon'disconnect' console.log'disconnect, endpoint:' ep;;// Handle monitor errorsocketon'monitor_error'console.log'Error in monitoring: %s, will restart monitoring in 5 seconds' err;setTimeout socketmonitor500 0; 5000;;// Call monitor, check for events every 500ms and get all available events.console.log'Start monitoring...';socketmonitor500 0;socketconnect'tcp://127.0.0.1:1234';setTimeoutconsole.log'Stop the monitoring...';socketunmonitor;20000;
$ git clone https://github.com/JustinTulloss/zeromq.node.git zmq && cd zmq$ npm i
# on unix:$ make# building on windows:> npm i
# on unix:$ make test# testing on windows:> npm t
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:
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
make perf will run the commands listed above.