Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining pub/sub with req/rep in zeromq

Tags:

zeromq

How can a client both subscribe and listen to replies with zeromq? That is, on the client side I'd like to run a loop which only receives messages and selectively sends requests, and on the server side I'd like to publish most of the time, but to sometimes receive requests as well. It looks like I'll have to have two different sockets - one for each mode of communication. Is it possible to avoid that and on the server side receive "request notifications" from the socket on a zeromq callback thread while pushing messages to the socket in my own thread?

like image 733
MockInterface Avatar asked Jul 16 '11 14:07

MockInterface


1 Answers

I am awfully new to ZeroMQ, so I'm not sure if what you want is considered best-practice or not. However, a solution using multiple sockets is pretty simple using zmq_poll.

The basic idea would be to have both client and server:

  • open a socket for pub/sub
  • open a socket for req/rep
  • multiplex sends and receives between the two sockets in a loop using zmq_poll in an infinite loop
  • process req/rep and pub/sub events within the loop as they occur

Using zmq_poll in this manner with multiple sockets is nice because it avoids threads altogether. The 0MQ guide has a good example here. Note that in that example, they use a timeout of -1 in zmq_poll, which causes it to block until at least one event occurs on any of the multiplexed sockets, but it's pretty common to use a timeout of x milliseconds or something if your loop needs to do some other work as well.

like image 190
bjlaub Avatar answered Sep 29 '22 18:09

bjlaub