• This repository has been archived on 24/Apr/2020
  • Stars
    star
    1,648
  • Rank 28,365 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 14 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js bindings to the zeromq library

zmq   Build Status Build status

ØMQ bindings for node.js.

Project Status

This project has moved!

The same people (and more) who brought you zmq now bring you zeromq, under the official zeromq GitHub organization.

We suggest all users migrate to this fork. Issues and Pull Requests on this (JustinTulloss/zeromq.node) repository will no longer be handled by the core team.

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.

More Repositories

1

cobra

Cobra is a small JavaScript library that adds some nice properties to the JavaScript class system while still leveraging prototypal inheritance. It is heavily influenced by Python, hence the name Cobra.
JavaScript
15
star
2

tokyo-cabinet-clj

A wrapper around the Tokyo Cabinet Java library for Clojure
Clojure
14
star
3

dropbox.js

Dropbox API client in JavaScript
JavaScript
7
star
4

flockr

A twitter portal page in Clojure.
JavaScript
7
star
5

hackon

A tool to manage environment variables for projects
Shell
6
star
6

yaps

Yet Another Python Server. Uses stackless tasklets to distribute requests around a self-organized network of servers. The requests are session-free, allowing a more natural web programming paradigm. Formed the basis of my undergraduate research.
Python
5
star
7

docker-goose

Shell
4
star
8

gogetter

Fetches open graph data for URLs
Go
3
star
9

service.js

A service provider abstraction in JS
JavaScript
3
star
10

0mq-playground

Examples using the zeromq library
C
3
star
11

daap-train

A daap server in python. Is a dead project, doesn't currently work.
Python
2
star
12

AeroMQ

A very simple task queue in CoffeeScript, running on Node.JS.
CoffeeScript
2
star
13

harelib

Asynchronous AMQP library for python, based off of py-amqplib and the tornado web server.
Python
2
star
14

TPS

Tab pub/sub. Allows JavaScript apps to communicate between multiple open tabs.
JavaScript
2
star
15

gobd

Go library for interacting with OBDII
Go
2
star
16

cdrvws

Command line URL shortener in Go
Go
2
star
17

blackjack

Just a toy command line blackjack program in Python
Python
2
star
18

blackbox

A wiimote driven music player designed for a settop box. It gets music from other computers on the local network.
C
2
star
19

Meetup.com-Mojo-Client

This is an example Palm webOS client for Meetup.com
JavaScript
2
star
20

discoversong

Python
2
star
21

harmonize.fm

The code from a long defunct startup
JavaScript
2
star
22

pinpoint

Clojure
1
star
23

hut

A simple service framework for Go
Go
1
star
24

cairo-playground

Just a place for me to learn cairo.
C
1
star
25

serf

A python clone of minion, a dead simple job queue for rabbitmq
Python
1
star
26

consistentkeys

Little util to aid in key distribution analysis
Python
1
star
27

astrcmp

A C library that computes the Levenshtein Distance between two strings.
C
1
star
28

White-Castle

Sliders in web pages.
1
star
29

jslogparse

simple utility for parsing javascript errors reported back to the server
Python
1
star
30

rflow

Script to make my github workflow easier without drinking the git-flow kool-aid
1
star
31

om-jibe

Synchronizes local state with the server in Om. Heavily inspired by om-sync.
1
star