Data source: ZeroMQ


1Presentation

This data source makes it possible to use the ZeroMQ network library, which facilitates data transfer by adding functionalities to conventional network sockets.

Important: ZeroMQ can handle several types of communication. These may be network connections ("TCP") between machines, inter-process connections ("IPC") within the same computer, or connections between threads within the same process ("inproc").
Because of specific implementation reasons (impossibility of sharing the ZeroMQ context), only TCP connections are handled here.

If you have set the connection parameters correctly, Temma will automatically create an object of type \Temma\Datasources\ZeroMQ. By convention, we'll assume that you've named this connection zmq in the etc/temma.php file (see configuration documentation).

The connection is then available in the controller by writing:

$this->zmq

In other objects managed by the dependency injection component, the connection is accessible by writing:

$loader->dataSources['zmq']

2Installation

To use this data source, you need to have installed the php-zmq extension.

Please refer to the PHP documentation or the documentation for your operating system. For example, under Ubuntu Linux, this extension and its dependencies are simply installed with the following command:

$ sudo apt install php-zmq

3Configuration

In the etc/temma.php file (see configuration documentation), you declare the DSN (Data Source Name) used to open the ZeroMQ socket.

When creating a ZeroMQ socket, you must first determine whether the socket will be listening for incoming connections, or whether it will connect to a ZeroMQ socket that is listening.

Listening sockets can listen on several different IP addresses and/or port numbers.
Their DSN is written as: zmq-bind://TYPE@SERVER:PORT[;SERVER:PORT…]

The DSN of connecting sockets is written as: zmq://TYPE@SERVER:PORT[;SERVER:PORT…]

  • TYPE: is the type of ZeroMQ socket to be created.
    • REQ: (request) send messages to a REP socket, and receive reply messages.
    • REP: (reply) receive messages sent by a REQ socket, and send messages in return (each received message must be followed by a reply).
    • PUSH: sends one-way messages to a PULL socket.
    • PULL: receives messages sent by a PUSH socket (without the option of replying).
    • PUB: (publish) send one-way messages to all connected SUB sockets.
    • SUB: (subscribe) receives messages sent by a PUB socket (without the option of replying).
  • SERVER: is the name of the server to connect to, or its IP address. For a listening socket, the server can be replaced by an asterisk (*) to listen on all the computer's network interfaces.
  • PORT: is the connection port number.

A ZeroMQ socket can listen on several network interfaces, or connect to several sockets at the same time. In the case of a listening socket, this allows you to listen on several different network interfaces or port numbers. In the case of a connecting socket, this allows you to exchange data with several servers at the same time.
In all cases, the ZeroMQ socket is handled in the same way, regardless of the number of network interfaces attached to it.
When there are several SERVER:PORT pairs, they are separated from each other by a semicolon character (;).

Examples:

  • zmq-bind://REP@127.0.0.1:5000
  • zmq://REQ@127.0.0.1:5000
  • zmq-bind://PULL@*:6000
  • zmq://PUSH@127.0.0.1:6000;other_server:6000
  • zmq-bind://PUB@127.0.0.1:6000;192.168.0.1:6500;10.0.0.1:7000
  • zmq://SUB@192.168.0.1:6500;external-service.com:8000

4Unified calls

4.1Array-type access

// write (serialized) data on the socket
$this->zmq[''] = $data;

// read (deserialized) data
$data = $this->zmq[''];

4.2Management of serialized data

// write (serialized) data on the socket
$this->zmq->set('', $data);

// write multiple (serialized) data
$this->zmq->mSet([
    $data1,
    $data2,
    $data3,
]);

// read (deserialized) data
$data = $this->zmq->get('');

4.3Raw data management

// write raw data
$this->zmq->write('', $str);

// écriture de plusieurs données (brutes)
$this->sock->mWrite({
    $str1,
    $str2,
    $str3,
]);

// read (raw) data
$str = $this->zmq->read('');