NOTE: This documentation refers to an old version of FFglitch.
The ZeroMQ interface has changed considerably since then.
This page is almost entirely wrong now.
ZeroMQ
ZeroMQ enables
ffgac,
ffedit, and
fflive
to do many kinds of network communication and messaging.
A global constructor ZMQ() is built-in to the quickjs engine.
This is a simple wrapper around ZeroMQ functionality.
This page is mostly based on the ZMQ API reference itself.
NOTE: The FFglitch ZeroMQ interface is still highly experimental.
It may change wildly without warning in newer versions.
ZMQ Constructor
The new ZMQ() constructor is used to create a new ZMQ object.
This is a wrapper around zmq_ctx_new() and 0MQ context.
Syntax
new ZMQ()
Return value
The new ZMQ object.
Examples
const zmq = new ZMQ();
ZMQ.prototype.socket()
This is a wrapper around zmq_socket().
NOTE: The resulting socket object is kept internally in the ZMQ
object because I still didn’t figure out a way to cleanup
properly without this hack.
Syntax
socket(type)
Parameters
The type argument specifies the socket type, which determines the semantics of communication over the socket.
The types are available in the global ZMQ object, i.e.: ZMQ.ZMQ_REQ.
Return value
The new ZMQSocket object or an exception.
Examples
const zmq = new ZMQ();
const zreq = zmq.socket(ZMQ.ZMQ_REQ);
ZMQ.prototype.set()
This is a wrapper around zmq_ctx_set().
Syntax
set(option_name, option_value)
Parameters
The zmq_ctx_set() function shall set the option specified by the
option_name argument to the value of the option_value argument.
The option names are available in the global ZMQ object, i.e.: ZMQ.ZMQ_BLOCKY.
Return value
0 on success, ortherwise an exception.
Examples
const zmq = new ZMQ();
zmq.set(ZMQ.ZMQ_BLOCKY, 0);
ZMQ.prototype.get()
This is a wrapper around zmq_ctx_get().
Syntax
get(option_name)
Parameters
The zmq_ctx_get() function shall return the option specified by the
option_name argument.
The option names are available in the global ZMQ object, i.e.: ZMQ.ZMQ_BLOCKY.
Return value
0 on success, ortherwise an exception.
Examples
const zmq = new ZMQ();
const max_msg_size = zmq.get(ZMQ.ZMQ_MAX_MSGSZ);
ZMQ.prototype.shutdown()
This is a wrapper around zmq_ctx_shutdown().
Syntax
shutdown()
Return value
0 on success, ortherwise an exception.
Examples
const zmq = new ZMQ();
zmq.shutdown();
ZMQSocket.prototype.bind()
This is a wrapper around zmq_bind().
Syntax
bind(endpoint)
Parameters
The endpoint is a string consisting of a transport:// followed by an address.
The transport specifies the underlying protocol to use.
The address specifies the transport-specific address to bind to.
Return value
0 on success, ortherwise an exception.
Examples
(sorry, I haven’t tested this)
ZMQSocket.prototype.unbind()
This is a wrapper around zmq_unbind().
Syntax
unbind(endpoint)
Parameters
The endpoint is a string consisting of a transport:// followed by an address.
The transport specifies the underlying protocol to use.
The address specifies the transport-specific address to unbind from.
Return value
0 on success, ortherwise an exception.
Examples
(sorry, I haven’t tested this)
ZMQSocket.prototype.connect()
This is a wrapper around zmq_connect().
Syntax
connect(endpoint)
Parameters
The endpoint is a string consisting of a transport:// followed by an address.
The transport specifies the underlying protocol to use.
The address specifies the transport-specific address to connect to.
Return value
0 on success, ortherwise an exception.
Examples
const zmq = new ZMQ();
const zreq = zmq.socket(ZMQ.ZMQ_REQ);
zreq.connect("tcp://localhost:5556");
ZMQSocket.prototype.disconnect()
This is a wrapper around zmq_disconnect().
Syntax
disconnect(endpoint)
Parameters
The endpoint is a string consisting of a transport:// followed by an address.
The transport specifies the underlying protocol to use.
The address specifies the transport-specific address to disconnect from.
Return value
0 on success, ortherwise an exception.
Examples
const zmq = new ZMQ();
const zreq = zmq.socket(ZMQ.ZMQ_REQ);
zreq.connect("tcp://localhost:5556");
zreq.disconnect("tcp://localhost:5556");
ZMQSocket.prototype.send()
This is a wrapper around zmq_send().
Syntax
send(buffer, flags)
Parameters
The buffer parameter is an Uint8FFArray.
The flags parameter is optional. It is ZMQ.ZMQ_DONTWAIT by default if not specified.
NOTE: I still didn’t implement helpers to convert between string to Uint8FFArray, but it’s on my TODO list.
Return value
0 on success, ortherwise an exception.
Examples
const zmq = new ZMQ();
const zreq = zmq.socket(ZMQ.ZMQ_REQ);
zreq.connect("tcp://localhost:5556");
const buf = new Uint8FFArray(16);
zreq.send(buf, 0); // flags=0 is blocking
zreq.disconnect("tcp://localhost:5556");
ZMQSocket.prototype.recv()
This is a wrapper around zmq_recv().
Syntax
recv(flags)
Parameters
The buffer parameter is an Uint8FFArray.
The flags parameter is optional. It is ZMQ.ZMQ_DONTWAIT by default if not specified.
NOTE: I still didn’t implement helpers to convert between string to Uint8FFArray, but it’s on my TODO list.
Return value
0 on success, ortherwise an exception.
Examples
const zmq = new ZMQ();
const zreq = zmq.socket(ZMQ.ZMQ_REQ);
zreq.connect("tcp://localhost:5556");
zreq.send(new Uint8FFArray(0)); // empty message
const data = zreq.recv();
if ( data )
{
console.log(`received ${data.length} bytes`);
if ( data.length != 0 )
console.log(JSON.stringify(data));
zreq.send(new Uint8FFArray(0)); // ack
}
zreq.disconnect("tcp://localhost:5556");