• Stars
    star
    1,371
  • Rank 33,047 (Top 0.7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 6 months 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 ØMQ library

ZeroMQ.js Next Generation

Latest version Greenkeeper monitoring Travis build status

⚠️ Version 6.0.0 (in beta) features a brand new API that solves many fundamental issues and is recommended for new projects. ⚠️

⚠️ For the current stable version see the 5.x branch ⚠️

ØMQ bindings for Node.js. The goals of this library are:

  • Semantically similar to the native ØMQ library, while sticking to JavaScript idioms.
  • Use modern JavaScript and Node.js features such as async/await and async iterators.
  • High performance.
  • Fully usable with TypeScript (3+).

Useful links

Table of contents

Installation

Install ZeroMQ.js with prebuilt binaries:

npm install [email protected]

Requirements for using prebuilt binaries:

  • Node.js 10.2+ or Electron 3+ (requires a N-API version 3+)

Prebuilt binaries

The following platforms have a prebuilt binary available:

  • Linux on x86-64 with libstdc++.so.6.0.21+ (glibc++ 3.4.21+), for example:
    • Debian 9+ (Stretch or later)
    • Ubuntu 16.04+ (Xenial or later)
    • CentOS 8+
  • Linux on x86-64 with musl, for example:
    • Alpine 3.3+
  • MacOS 10.9+ on x86-64
  • Windows on x86/x86-64

If a prebuilt binary is not available for your platform, installing will attempt to start a build from source.

Building from source

If a prebuilt binary is unavailable or if you want to pass certain options during build, you can build this package from source.

Make sure you have the following installed before attempting to build from source:

  • Node.js 10+ or Electron 3+
  • A working C++17 compiler toolchain with make
  • Python 3 with Node 12.13+ (or legacy Python 2.7)
  • CMake 2.8+
  • curl

To install from source:

npm install [email protected] --build-from-source

If you want to link against a shared ZeroMQ library, you can build skip downloading libzmq and link with the installed library instead as follows:

npm install [email protected] --zmq-shared

If you wish to use any DRAFT sockets then it is also necessary to compile the library from source:

npm install [email protected] --zmq-draft

Examples

Note: These examples assume the reader is familiar with ZeroMQ. If you are new to ZeroMQ, please start with the ZeroMQ documentation.

More examples can be found in the examples directory.

Push/Pull

This example demonstrates how a producer pushes information onto a socket and how a worker pulls information from the socket.

producer.js

Creates a producer to push information onto a socket.

const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Push

  await sock.bind("tcp://127.0.0.1:3000")
  console.log("Producer bound to port 3000")

  while (true) {
    await sock.send("some work")
    await new Promise(resolve => { setTimeout(resolve, 500) })
  }
}

run()

worker.js

Creates a worker to pull information from the socket.

const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Pull

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

  for await (const [msg] of sock) {
    console.log("work: %s", msg.toString())
  }
}

run()

Pub/Sub

This example demonstrates using zeromq in a classic Pub/Sub, Publisher/Subscriber, application.

publisher.js

Create the publisher which sends messages.

const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Publisher

  await sock.bind("tcp://127.0.0.1:3000")
  console.log("Publisher bound to port 3000")

  while (true) {
    console.log("sending a multipart message envelope")
    await sock.send(["kitty cats", "meow!"])
    await new Promise(resolve => { setTimeout(resolve, 500) })
  }
}

run()

subscriber.js

Create a subscriber to connect to a publisher's port to receive messages.

const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Subscriber

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

  for await (const [topic, msg] of sock) {
    console.log("received a message related to:", topic, "containing message:", msg)
  }
}

run()

Req/Rep

This example illustrates a request from a client and a reply from a server.

client.js

const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Request

  sock.connect("tcp://127.0.0.1:3000")
  console.log("Producer bound to port 3000")

  await sock.send("4")
  const [result] = await sock.receive()

  console.log(result)
}

run()

server.js

const zmq = require("zeromq")

async function run() {
  const sock = new zmq.Reply

  await sock.bind("tcp://127.0.0.1:3000")

  for await (const [msg] of sock) {
    await sock.send(2 * parseInt(msg, 10))
  }
}

run()

TypeScript

This library provides typings for TypeScript version 3.0.x and later.

Requirements

  • For TypeScript version >= 3:
  • For TypeScript version < 3.6:
    • either set compilerOptions.target to esnext or later (e.g. es2018)
    • or add the following, or similar, libraries to compilerOptions.lib (and include their corresponding polyfills if needed): es2015, ESNext.AsyncIterable

Example Usage

import { Request } from "zeromq"
// or as namespace
import * as zmq from "zeromq"

const reqSock = new Request()
//...
const repSock = new zmq.Reply()

More examples

More advanced examples can be found in the examples directory of this repository.

Or you can browse the API reference documentation to see all socket types, methods & options as well as more detailed information about how to apply them.

Compatibility layer for version 4/5

The next generation version of the library features a compatibility layer for ZeroMQ.js versions 4 and 5. This is recommended for users upgrading from previous versions.

Example:

const zmq = require("zeromq/v5-compat")

const pub = zmq.socket("pub")
const sub = zmq.socket("sub")

pub.bind("tcp://*:3456", err => {
  if (err) throw err

  sub.connect("tcp://127.0.0.1:3456")

  pub.send("message")

  sub.on("message", msg => {
    // Handle received message...
  })
})

Contribution

If you are interested in making contributions to this project, please read the following sections.

Dependencies

In order to develop and test the library, you'll need the tools required to build from source (see above).

Additionally, having clang-format is strongly recommended.

Defining new options

Socket and context options can be set at runtime, even if they are not implemented by this library. By design, this requires no recompilation if the built version of ZeroMQ has support for them. This allows library users to test and use options that have been introduced in recent versions of ZeroMQ without having to modify this library. Of course we'd love to include support for new options in an idiomatic way.

Options can be set as follows:

const {Dealer} = require("zeromq")

/* This defines an accessor named 'sendHighWaterMark', which corresponds to
   the constant ZMQ_SNDHWM, which is defined as '23' in zmq.h. The option takes
   integers. The accessor name has been converted to idiomatic JavaScript.
   Of course, this particular option already exists in this library. */
class MyDealer extends Dealer {
  get sendHighWaterMark(): number {
    return this.getInt32Option(23)
  }

  set sendHighWaterMark(value: number) {
    this.setInt32Option(23, value)
  }
}

const sock = new MyDealer({sendHighWaterMark: 456})

When submitting pull requests for new socket/context options, please consider the following:

  • The option is documented in the TypeScript interface.
  • The option is only added to relevant socket types, and if the ZMQ_ constant has a prefix indicating which type it applies to, it is stripped from the name as it is exposed in JavaScript.
  • The name as exposed in this library is idiomatic for JavaScript, spelling out any abbreviations and using proper camelCase naming conventions.
  • The option is a value that can be set on a socket, and you don't think it should actually be a method.

Testing

The test suite can be run with:

npm install
npm run build
npm run test

The test suite will validate and fix the coding style, run all unit tests and verify the validity of the included TypeScript type definitions.

Some tests are not enabled by default:

  • API Compatibility tests from ZeroMQ 5.x have been disabled by default. You can include the tests with INCLUDE_COMPAT_TESTS=1 npm run test
  • Some transports are not reliable on some older versions of ZeroMQ, the relevant tests will be skipped for those versions automatically.

Publishing

To publish a new version, run:

npm version <new version>
git push && git push --tags

Wait for continuous integration to finish. Prebuilds will be generated for all supported platforms and attached to a Github release. Documentation is automatically generated and committed to gh-pages. Finally, a new NPM package version will be automatically released.

History

Version 6+ is a complete rewrite of previous versions of ZeroMQ.js in order to be more reliable, correct, and usable in modern JavaScript & TypeScript code as first outlined in this issue. Previous versions of ZeroMQ.js were based on zmq and a fork that included prebuilt binaries.

See detailed changes in the CHANGELOG.

More Repositories

1

libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
C++
8,996
star
2

pyzmq

PyZMQ: Python bindings for zeromq
Python
3,480
star
3

netmq

A 100% native C# implementation of ZeroMQ for .NET
C#
2,822
star
4

jeromq

Pure Java ZeroMQ
Java
2,288
star
5

cppzmq

Header-only C++ binding for libzmq
C++
1,737
star
6

czmq

High-level C binding for ØMQ
C
1,110
star
7

zmq.rs

A native implementation of ØMQ in Rust
Rust
1,015
star
8

zyre

Zyre - an open-source framework for proximity-based peer-to-peer applications
C
843
star
9

jzmq

Java binding for ZeroMQ
Java
584
star
10

goczmq

goczmq is a golang wrapper for CZMQ.
Go
552
star
11

php-zmq

ZeroMQ for PHP
C
543
star
12

zeromq4-x

ØMQ 4.x stable release branch - bug fixes only
C++
446
star
13

zmqpp

0mq 'highlevel' C++ bindings
C++
418
star
14

zeromq2-x

ØMQ/2.x distribution
C++
365
star
15

malamute

The ZeroMQ Enterprise Messaging Broker
C
311
star
16

gomq

Pure Go Implementation of a Subset of ZeroMQ
Go
290
star
17

clrzmq

CLR (.NET & Mono) binding for 0MQ
C#
272
star
18

filemq

FileMQ is a publish-subscribe file service based on 0MQ
Java
271
star
19

rbzmq

Ruby binding for 0MQ
C
248
star
20

clrzmq4

ZeroMQ C# namespace (.NET and mono, Windows, Linux and MacOSX, x86 and amd64)
C#
235
star
21

zproto

A protocol framework for ZeroMQ
C
228
star
22

zeromq3-x

ØMQ/3.2 release branch - bug fixes only
C++
228
star
23

JSMQ

Javascript client for ZeroMQ/NetMQ
JavaScript
190
star
24

chumak

Pure Erlang implementation of ZeroMQ Message Transport Protocol.
Erlang
189
star
25

erlzmq2

Erlang binding for 0MQ (v2)
C
165
star
26

libcurve

An encryption and authentication library for ZeroMQ applications
C
161
star
27

zproject

CLASS Project Generator
Shell
142
star
28

lzmq

Lua binding to ZeroMQ
Lua
133
star
29

gitdown

Turn github into your publishing platform
Perl 6
130
star
30

zeromq4-1

ZeroMQ 4.1.x stable release branch - bug fixes only
C++
125
star
31

pyre

Python port of Zyre
Python
116
star
32

exzmq

ZeroMQ for Elixir
Elixir
115
star
33

fszmq

An F# binding for the ZeroMQ distributed computing library. For more information, please visit:
F#
112
star
34

majordomo

Majordomo Project
C
111
star
35

cljzmq

Clojure bindings for ØMQ
Clojure
105
star
36

rfc

ZeroMQ RFC project
C
104
star
37

dafka

Dafka is a decentralized distributed streaming platform
C
101
star
38

gyre

Golang port of Zyre
Go
87
star
39

zwssock

ZeroMQ WebSocket library for CZMQ
C
85
star
40

jszmq

Javascript port of zeromq
TypeScript
76
star
41

libzmtp

Minimal ZMTP implementation in C
C
53
star
42

ingescape

Model-based framework for broker-free distributed software environments. Any language, any OS, web, cloud.
C
52
star
43

zbroker

Elastic pipes
C
50
star
44

czmqpp

C++ wrapper for czmq. Aims to be minimal, simple and consistent.
C++
43
star
45

zeromq.org

ZeroMQ Website
HTML
32
star
46

cookbook

ZeroMQ Cookbook
Python
32
star
47

pyczmq

Python CZMQ bindings
Python
31
star
48

zebra

REST/HTTP to XRAP gateway
C++
26
star
49

zmtpdump

ZeroMQ Transport Protocol packet analyzer
Roff
25
star
50

jeromq-jms

JeroMQ JMS
Java
24
star
51

jzmq-api

A Java ØMQ API for abstracting the various implementations of ZeroMQ Message Transport Protocol
Java
24
star
52

zmq-jni

Simple High Performance JNI Wrapper for ØMQ
Java
22
star
53

perlzmq

version agnostic Perl bindings for zeromq
Perl
22
star
54

zmtp

Stuff related to the ZMTP protocol
C
18
star
55

contiki-zmtp

ZMTP for Contiki OS
C
16
star
56

jyre

Java implementation of ZRE protocol
Java
14
star
57

zccp

ZeroMQ Command & Control Protocol
Go
14
star
58

f77_zmq

Fortran binding for ZeroMQ
C
13
star
59

ztools

Tools for ØMQ auto-builds and API site
Perl
12
star
60

zeps

ZeroMQ Enterprise Publish-Subscribe (ZEPS) **DEPRECATED**
C
12
star
61

mruby-zmq

mruby bindings for libzmq (v4)
C
10
star
62

zeromq2-0

Packaging project for ØMQ/2.0 series
C++
9
star
63

zkernel

z kernel
C
9
star
64

nimczmq

Nim ( http://nim-lang.org/ ) bindings for CZMQ
Nim
6
star
65

curvezmq-java

Java
5
star
66

gozyre

Go bindings for zeromq libzyre - an open-source framework for proximity-based peer-to-peer applications
Go
5
star
67

issues

Issue test cases
C
4
star
68

zdiscgo

CZMQ service discovery zactor with support for Go plugins.
C
4
star
69

jdafka

Dafka protocol implementation in Java
Java
4
star
70

zlabs

Labs project for experimenting with CZMQ
Shell
4
star
71

zeromq-buildbot

A buildbot based regression tester for Zeromq
Python
3
star
72

azmq1-0

v1.0 release of azmq
C++
3
star
73

lyre

Lua port of Zyre
Lua
3
star
74

jeromq3-x

Java
3
star
75

jzmq3-x

Java
2
star
76

zmtp-java

Java
2
star
77

libzmq-relicense

This repo contains information regarding re-licensing libzmq
Python
2
star
78

clrzmq2

Old repository path for clrzmq
2
star
79

libzmq-fuzz-corpora

fuzzers corpus files for libzmq are stored in binary format in this repository
1
star
80

zeromq-download-redirect

HTML
1
star