Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZMQ: Multiple request/reply-pairs

Tags:

zeromq

ZeroMQs Pub/Sub pattern makes it easy for the server to reply to the right client. However, it is less obvious how to handle communication that cannot be resolved within two steps, i.e. protocols where multiple request/reply pairs are necessary.

For example, consider a case where the client is a worker which asks the server for new work of a specific type, the server replies with the parameters of the work, the client then sends the results and the server checks these and replies whether they were correct.

Obviously, I can't just use recv,send,recv,send sequentially and assume that the first and the second recv are from the same client. What would be the idiomatic way to use multiple recv,send pairs without having to handle messages from other clients inbetween?

like image 932
DaVinci Avatar asked Apr 02 '15 19:04

DaVinci


1 Answers

Multiple Request/Reply pairs can be made through the use of ZMQ_ROUTER sockets. I recommend using ZMQ_REQ sockets on the clients for bidirectional communication.

If you want to have multiple clients accessing a single server you could use a router socket on the server and request sockets on the clients.

Check out the ZMQ guide's section on this pattern: http://zguide.zeromq.org/php:chapter3#The-Asynchronous-Client-Server-Pattern

All the clients will interact with the server in the same pattern as Pub/Subs except they will all point at a single server Router socket.

The server on the other hand will receive three messages for every single message a client sends. These parts represent:

  • Part0 = Identity of connection (random number of which client it is)
  • Part1 = Empty frame
  • Part2 = Data of the ZMQ message.

Reference: http://zguide.zeromq.org/php:chapter3#ROUTER-Broker-and-REQ-Workers

The identity can be used to differentiate between clients accessing on a single port. Repacking the message in the same order and responding on the router socket (with a different data frame) will automatically route it to the client who sent the message.

like image 116
Matt Thomson Avatar answered Sep 30 '22 12:09

Matt Thomson