Using Sockets to Carry Data

To send and receive messages, you use the zmq_msg_send() and zmq_msg_recv() methods. The names are conventional, but ØMQ’s I/O model is different enough from the TCP model (Figure 2-1) that you will need time to get your head around it.

TCP sockets are 1-to-1

Figure 2-1. TCP sockets are 1-to-1

Let’s look at the main differences between TCP sockets and ØMQ sockets when it comes to working with data:

  • ØMQ sockets carry messages, like UDP, rather than a stream of bytes as TCP does. A ØMQ message is length-specified binary data. We’ll come to messages shortly; their design is optimized for performance and so a little tricky.

  • ØMQ sockets do their I/O in a background thread. This means that messages arrive in local input queues and are sent from local output queues, no matter what your application is busy doing.

  • ØMQ sockets have 1-to-N routing behavior built in, according to the socket type.

The zmq_msg_send() method does not actually send the message to the socket connection(s). It queues the message so that the I/O thread can send it asynchronously. It does not block except in some exception cases. So, the message is not necessarily sent when zmq_msg_send() returns to your application.

Get ZeroMQ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.