Dimitar Kostov ramblings

Messaging patterns in ZeroMQ

In the previous post I’ve outlined what to expect from ZeroMQ. In this is post I’ll focus on infrastructure and messaging patterns options ZeroMQ gives us to play with.


ZeroMQ gives us the choice of how to organize your “network”. In the simple case it’s a “server” who binds to a socket and a “client” who connects to the “server”. The “client” sends a message and the “server” receives it.

It’s possible to bind a socket to an address and at the same time to connect to other end point. This means that we can use it as a forwarder:

Those examples use pipeline messaging pattern and it’s to time to explain how to handle the message flow.

Messaging patterns

ZeroMQ supports four messaging patterns:

  1. Pipeline - used for distributing data to nodes arranged in a pipeline
  2. Request-Reply - used for sending requests from a client to one or more instances of a service, and receiving subsequent replies to each request sent
  3. Publish-Subscribe - used for sending requests from a client to one or more instances of a service, and receiving subsequent replies to each request sent
  4. Exclusive Pair - advanced pattern used for communicating exclusively between two peers


We can see the usage of pipeline pattern in the previous example. ZMQ::UPSTREAM(ZMQ::PULL) socket only gets data, ZMQ::DOWNSTREAM(ZMQ::PUSH) socket pushes date.

In more complicated manner, a pipeline stage can connect to multiple nodes. If so data is automatically load-balanced among all connected nodes.

NOTICE: ZMQ::DOWNSTREAM/ZMQ::UPSTREAM are deprecated. The new names of the socket types are ZMQ::PUSH/ZMQ::PULL. Right now the names are not updated in zmq ruby gem, but in ffi version everything is working correctly.


  Making request

  1. create ZMQ::REQ socket and connect to server
  2. send a message using send method
  3. capture reply with recv method

  Accepting request and sending reply

  1. create ZMQ::REP socket and bind on specific address/port
  2. receive message with recv method
  3. send a reply with send method

It’s also possible to make more than one request per socket. Just use connect on different addresses/ports.

In the following example we’re sending 10 numbers as 10 separate requests, the server get the request(the number), multiply it and then send it back to the client.


It’s the well know pattern. And this is how it works in ZeroMQ


  1. Create a ZMQ::PUB socket and bind it on address/port
  2. Publish data using send


  1. Create a ZMQ::SUB socket and connect it to the publisher
  2. Set the filter of the received messages
  3. Receive messages with recv

Let assume that we have news for the revenue of Microsoft, Apple, Google and Oracle. Our publisher will broadcast news for all of these companies, but our subscriber is interested only in Google’s revenue:

Using setsockopt method we set the filter to get only news about Google. If we don’t provide filter, the subscriber wont’ get any news. If we set the filter to empty string, the subscriber will get all the news form the publisher.

Exclusive Pair

Simple enoughbidirectional communication, no routing or filtering. Looks like regular socket. ZMQ::PAIR socket can only be connected to a single peer.

Wrapping up

In the next chapter I’ll use the knowledge of this one to write a simple Hello World app in Ruby - a chat server. Stay tuned.

PS: Feel free to write me at mitko.kostov[at]gmail.com if you find some rubbish statement.