We have our first basic request-reply architecture, as shown in the following diagram:
The request-reply pattern
Let's have a closer look at the code to understand how it works.
First we create a context and a socket. The zmq_ctx_new() method creates a new
context. It is thread safe, so one context can be used from multiple threads.
zmq_socket(2) creates a new socket in the defined context. ZeroMQ sockets
are not thread safe, so it should be used only by the thread where it was created.
Traditional sockets are synchronous whereas ZeroMQ sockets have a queue on
the client side and another on the server side for managing the request-reply
pattern asynchronously. ZeroMQ automatically arranges setting up the connection,
reconnecting, disconnecting, and content delivery. We will cover the difference
between traditional sockets and ZeroMQ sockets in depth in Chapter 3 , Using
Socket Topology .
The server binds the ZMQ_REP socket to port 4040 and starts waiting for requests
and replies back whenever it receives a message.
This basic "hello world" example introduces us to our first pattern, the request-reply