Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. ### ZeroMQ Hub pattern
  2.  
  3. Say you have a group of nodes, and you want to ask them all a question, like (to keep it simple) "who's here?". The hub pattern is an easy way to have N nodes answer one node's question, using ZeroMQ, a single endpoint and without using any central forwarding device. The caveats are that this is not suited for quick messaging because of the many reconnects and can be quite unstable for that same reason. The ease of programming it and the low-maintenance and simplicity of the solution still makes it worth using though.
  4.  
  5. Beware that your ZeroMQ version sould be up to date (at least 4 I think), because we use the new feature of XSUB sockets talking back to publishers.
  6.  
  7. The pattern is set up as follows:
  8.  
  9. We have a network of N nodes. Each of these connects to the central endpoint using an XSUB socket, and waits to receive a question message on that socket. In your implementation you may use the subscription mechanism to filter out only questions you can answer, otherwise you should subscribe to all messages.
  10. At some point, one node might decide to step up and ask everyone a question. To do this, the node binds an XPUB socket on the same endpoint, waits a while until it figures everyone is connected, and sends out it's question message. After this the node waits again, for whatever it believes is a reasonable time for everyone to answer.
  11. Once a listening node receives the question, it can formulate an answer (that should not start with 0 or 1, because of the subscription mechanism) if it wishes to, and send this back on the XSUB socket. After the asking node is done waiting it collects the messages received as answers, and unbinds the socket so it is free for another node to ask a question.
  12.  
  13. This pattern is pretty easy to implement, and seems to be stable on current versions of ZeroMQ. The main weak point is that only one node can ask a question at a time, and it might take quite a bit of waiting if you want to be sure everyone received the question and you've received all the answers. If you're okay with being able to ask one question per second or so, though, you should be okay. I originally developed this as an easy way to do service discovery, so for that purpose it worked quite well.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement