Skip to content
Jennifer Programming Language

zmq API reference

A ZeroMQ peer over the net system library: ZMTP 3.1 (RFC 37), the wire protocol libzmq, pyzmq, JeroMQ and the other ZeroMQ stacks speak over TCP, so a Jennifer program can talk to them. Socket types PAIR, PUB / SUB, REQ / REP, DEALER / ROUTER and PUSH / PULL, multipart messages as list of bytes, and the NULL (no security) and PLAIN (username / password, sent in clear text) mechanisms. CURVE encryption is not supported.

Unlike libzmq, a socket is one connection to one peer, and nothing happens in the background: connect dials a peer, listen + accept take peers one at a time (spawn a task per accepted socket to serve several), and the program reads with receive or poll (with a timeout). There is no automatic reconnect and no send queue: a send writes to the connection, and fails if it is gone. Needs the default jennifer binary (uses net).

Every failure throws a zmq.ZmqError (kind "zmq"); a peer that refuses the handshake with an ERROR command puts its reason in peerReason. A frame over the size cap is kind "limit".

Import with import "zmq.j" as zmq;. See the zmq guide for prose and examples.

Contents

NameWhat it is
acceptWait for the next peer to connect, run the handshake, and return a socket for it.
addressThe address a listener is bound to, "host:port" - the real port after listening on ":0".
closeClose a socket's connection.
closeListenerStop listening.
connectConnect a socket of type kind to a peer listening at address and run the handshake, with the default op...
connectWithConnect like connect, with options (a PLAIN client, an identity, a timeout).
defaultOptionsThe default options: NULL mechanism, no identity, a 5 s timeout and a 64 MiB message cap.
listenListen for peers at address with the default options.
listenWithListen like listen, with options.
pollWait up to timeoutMs for a message.
receiveBlock until the next message arrives and return it.
replyAnswer a request received on a REP or ROUTER socket.
sendSend a message, one frame per list element.
sendTextSend a one-frame text message (UTF-8), with send's rules.
subscribeA copy of a SUB socket that receives messages whose first frame starts with prefix ("" subscribes to ever...
textThe first frame of a message as UTF-8 text.
unsubscribeA copy of a SUB socket with one subscription to prefix removed (the publisher is told).
Listenerstruct: A listening socket from listen / listenWith; accept takes one peer from it.
Messagestruct: One received message.
Optionsstruct: Connection settings for connectWith / listenWith.
Socketstruct: An open connection to one peer, after the ZMTP handshake.
ZmqErrorstruct: A failed ZeroMQ exchange.

Functions

accept

jennifer
zmq.accept(l as Listener)

Wait for the next peer to connect, run the handshake, and return a socket for it. A peer whose handshake fails (wrong mechanism, bad PLAIN credentials, incompatible socket type) is disconnected and the call throws; call accept again for the next one.

l
the listener
Returns Socket
the socket for the new peer
Throws ZmqError
kind "zmq" if the peer's handshake fails

address

jennifer
zmq.address(l as Listener)

The address a listener is bound to, "host:port" - the real port after listening on ":0".

l
the listener
Returns string
the bound address

close

jennifer
zmq.close(s as Socket)

Close a socket's connection.

s
the socket

closeListener

jennifer
zmq.closeListener(l as Listener)

Stop listening.

l
the listener

connect

jennifer
zmq.connect(kind as string, address as string)

Connect a socket of type kind to a peer listening at address and run the handshake, with the default options.

kind
the socket type: "PAIR", "PUB", "SUB", "REQ", "REP", "DEALER", "ROUTER", "PULL" or "PUSH"
address
the peer, "host:port" (no "tcp://" prefix)
Returns Socket
the connected socket
Throws ZmqError
kind "zmq" for an unknown type, a handshake failure or an incompatible peer; a net error (kind "timeout", "closed", or a net.NetError, kind "net") if the connection fails

connectWith

jennifer
zmq.connectWith(kind as string, address as string, opts as Options)

Connect like connect, with options (a PLAIN client, an identity, a timeout).

kind
the socket type
address
the peer, "host:port"
opts
the options
Returns Socket
the connected socket
Throws ZmqError
kind "zmq" for an unknown type, bad options, a refused or failed handshake; a net error if the connection fails

defaultOptions

jennifer
zmq.defaultOptions()

The default options: NULL mechanism, no identity, a 5 s timeout and a 64 MiB message cap. Set fields on the result for PLAIN or an identity.

Returns Options
the defaults

listen

jennifer
zmq.listen(kind as string, address as string)

Listen for peers at address with the default options. Each accept takes one peer and returns a socket of type kind for it.

kind
the socket type of every accepted socket
address
where to listen, "host:port" (":0" picks a free port)
Returns Listener
the listener

listenWith

jennifer
zmq.listenWith(kind as string, address as string, opts as Options)

Listen like listen, with options. For PLAIN, the listener is the server: it accepts a peer only if it sends opts.username and opts.password.

kind
the socket type of every accepted socket
address
where to listen, "host:port"
opts
the options every accepted socket gets
Returns Listener
the listener

poll

jennifer
zmq.poll(s as Socket, timeoutMs as int)

Wait up to timeoutMs for a message. Returns a list of zero or one Message: empty when nothing arrived in time (or only a command or a dropped message did), one Message otherwise. Once a message has started arriving, the rest of it must come within the socket's timeoutMs option. A timeoutMs of 0 is a non-blocking check: it returns what has already arrived (waiting at most 1 ms).

s
the socket
timeoutMs
how long to wait, in milliseconds (0 = do not wait)
Returns list of Message
zero or one message
Throws ZmqError
kind "zmq" for a negative timeoutMs or a socket type that cannot receive

receive

jennifer
zmq.receive(s as Socket)

Block until the next message arrives and return it. Messages the socket type drops - a SUB message no subscription matches, a malformed REQ / REP envelope - are skipped. A PING from the peer is answered on the way.

s
the socket
Returns Message
the next message
Throws ZmqError
kind "zmq" if the peer reports an error or the socket type cannot receive; kind "limit" for a message over maxMessageBytes; kind "closed" when the peer disconnects

reply

jennifer
zmq.reply(s as Socket, request as Message, frames as list of bytes)

Answer a request received on a REP or ROUTER socket. On REP the request's envelope (the routing frames and the empty delimiter) goes back in front of frames, so the reply reaches its requester through any ROUTER in between. On ROUTER the request's routing id selects the peer and frames are sent as they are - include the empty delimiter frame yourself when the peer is a REQ.

s
the REP or ROUTER socket
request
the request being answered
frames
the reply frames (at least one)

send

jennifer
zmq.send(s as Socket, frames as list of bytes)

Send a message, one frame per list element. A REQ socket adds the empty delimiter frame REP peers expect. A ROUTER socket takes the peer's routing id as the first frame (as libzmq's ROUTER does) and does not send it. REP sockets answer with reply; SUB and PULL sockets cannot send.

s
the socket
frames
the message frames (at least one)
Throws ZmqError
kind "zmq" if the socket type cannot send, the message is empty, or (ROUTER) the routing id is not the peer's

sendText

jennifer
zmq.sendText(s as Socket, text as string)

Send a one-frame text message (UTF-8), with send's rules.

s
the socket
text
the message

subscribe

jennifer
zmq.subscribe(s as Socket, prefix as string)

A copy of a SUB socket that receives messages whose first frame starts with prefix ("" subscribes to everything). The subscription is sent to the publisher, which may then filter at its end; receive filters too. A SUB socket with no subscription receives nothing.

s
the SUB socket
prefix
the topic prefix
Returns Socket
the socket with the subscription added

text

jennifer
zmq.text(m as Message)

The first frame of a message as UTF-8 text.

m
the message
Returns string
the text ("" for a message with no frames)

unsubscribe

jennifer
zmq.unsubscribe(s as Socket, prefix as string)

A copy of a SUB socket with one subscription to prefix removed (the publisher is told). Removing a prefix that was never subscribed does nothing.

s
the SUB socket
prefix
the topic prefix
Returns Socket
the socket with the subscription removed

Structs

Listener

A listening socket from listen / listenWith; accept takes one peer from it.

jennifer
def struct Listener {
    listener as net.Listener,
    kind as string,
    opts as Options
};
listener
the TCP listener
kind
the socket type every accepted socket gets
opts
the options every accepted socket gets

Message

One received message. frames is the message body. envelope is what a REP or ROUTER socket needs to answer it with reply: the routing frames a REP socket stripped, or for a ROUTER the peer's routing id. It is empty for the other socket types.

jennifer
def struct Message {
    frames as list of bytes,
    envelope as list of bytes
};
frames
the message frames, in order
envelope
the reply envelope (REP / ROUTER only)

Options

Connection settings for connectWith / listenWith. defaultOptions() gives the NULL mechanism, no identity, a 5 s timeout and a 64 MiB message cap; a zero numeric field also takes its default.

jennifer
def struct Options {
    mechanism as string,
    username as string,
    password as string,
    identity as bytes,
    timeoutMs as int,
    maxMessageBytes as int
};
mechanism
the security mechanism: "NULL" (or "") or "PLAIN"; both peers must use the same one
username
PLAIN: the username a client sends, or the one a listener accepts
password
PLAIN: the password a client sends, or the one a listener accepts
identity
the routing id a ROUTER peer sees for this REQ / DEALER / ROUTER socket (empty: the ROUTER picks one); at most 255 bytes, not starting with a zero byte
timeoutMs
how long to wait for a connection and its handshake, and for the rest of a message once poll has seen its start, in milliseconds (0 = 5000)
maxMessageBytes
the largest message accepted from the peer, all frames together (0 = 64 MiB); a larger one throws Error{kind: "limit"}

Socket

An open connection to one peer, after the ZMTP handshake. Value-semantic, but every copy shares the one connection.

jennifer
def struct Socket {
    conn as net.Conn,
    kind as string,
    peerKind as string,
    peerIdentity as bytes,
    peerMinor as int,
    timeoutMs as int,
    maxMessageBytes as int,
    subscriptions as list of bytes
};
conn
the TCP connection
kind
this socket's type ("REQ", "SUB", ...)
peerKind
the peer's socket type, from its handshake
peerIdentity
the peer's routing id: the identity it sent, or for a ROUTER one generated when it sent none
peerMinor
the peer's ZMTP minor version (0 or 1; decides how subscriptions are sent)
timeoutMs
the timeout from the options, in milliseconds
maxMessageBytes
the message size cap from the options
subscriptions
a SUB socket's topic prefixes (see subscribe)

ZmqError

A failed ZeroMQ exchange. Every error of kind "zmq" is one, so its fields are safe to read once the kind matches. (A frame over the size cap is kind "limit".)

jennifer
def struct ZmqError {
    kind as string,
    message as string,
    peerReason as string
};
kind
always "zmq"
message
zmq: and what went wrong
peerReason
the reason of the peer's ERROR command when it refused the handshake (a ZAP authentication failure, an unsupported mechanism, ...); "" for any other failure