Introduction
ZeroMQ is a high-performance asynchronous messaging library to use in distributed or concurrent applications. It provides a message queue and can run without a dedicated message broker.
The focus of ZeroMQ is on small messages sent over relatively small number of stable communication channels.
It supports common messaging patterns (pub/sub, request/reply, client/server and others) over a variety of transports (TCP, in-process, inter-process, multicast, WebSocket and more), making inter-process messaging as simple as inter-thread messaging.
However,
ZeroMQ offers several communication patterns (like request-response, publish-subscribe, etc.), but the user is expected to know up-front which pattern will be used with the given connection - and commit to this pattern. For example, if the user opens a request-response connection, it cannot be used for publish-subscribe communication. And the other way round. In other words, the communication pattern is a property of the connection and is fixed when the connection is created.
Some Features of ZeroMQ ::
1.ZeroMQ was designed to get the highest throughput possible making it one of the quickest.
2.It achieves its high throughput with the help of message batching, which allows to send several consecutive messages as a single unit, thus reducing the overhead that is associated with packet headers and other low-level details.
3.ZeroMQ offers relatively good throughput but it does sequential ordering. It means that there is no place for the concept of completion of more important tasks first,it works on FIFO policy.
4. ZeroMQ offers no data model at all and the user is expected to handle message content serialization.
5.Hence its gets a bit tricky with more complex data(like very large strings or messages) or when communicating machines have different byte ordering etc.
Sample Program of Hello World with Client and Server using ZeroMQ
// Hello World server
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *respond = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (respond, "tcp://*:55");
assert (rc == 0);
while (1) {
char buffer [5];
zmq_recv (respond, buffer, 5, 0);
printf ("Received Hello\n");
sleep (1); //wait
zmq_send (respond, "World", 5, 0);
}
return 0;
}
The server creates a socket of type response binds it to port 55 and then waits for messages.
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
printf ("Connecting server..%u2026\n");
void *context = zmq_ctx_new ();
void *request = zmq_socket (context, ZMQ_REQ);
zmq_connect (request, "tcp://localhost:55");
int request_nbr;
for (request_nbr = 0; request_nbr != 5; request_nbr++) {
char buffer [5];
printf ("Sending Hello %d%u2026\n", request_nbr);
zmq_send (request, "Hello", 5, 0);
zmq_recv (request, buffer, 5, 0);
printf ("Received World %d\n", request_nbr);
}
zmq_close (request); //required
zmq_ctx_destroy (context); //required
return 0;
}
The client creates a socket of type request, connects and starts sending messages.
The ZeroMQ library allows a different approach with smart endpoints and dumb pipes. Here it was shown how easily the library can be used as an example.
Comments