Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ, Client<-> Server , bi-directional communication possible with only having the client connect to host?

I am facing the following problem:

I have a client (ultimately n-clients) and like to connect to a server. Clients know the server/host address but the server does not know the address of the client(s). I like to be able to accomplish the following messaging patterns between client-server (both, the client and the server need to be able to accomplish the following message patterns):

  • Publish Messages (no reply expected)
  • Receive Messages (no reply expected)
  • Request / Receive Messages (reply expected)
  • Stream messages (this may be redundant as it may be served through the publish message pattern above)

Again the important point, and where I struggle is how to connect to the host while still being able to send AND receive messages. The host has no ability to connect to clients, it can only accept client connection requests. Please note that I do not look for a solution with proxy/broker to which both client and server connect otherwise I could go directly with solutions such as rabbitmq.

How can I best accomplish this, even better with reference to code samples.

Thanks a lot.

like image 739
Matt Avatar asked Jun 08 '13 12:06

Matt


People also ask

What is ZeroMQ used for?

ZeroMQ provides a whole slew of language APIs which run on most operating systems and allows you to communicate seamlessly between all sorts of programs. It also provides a collection of patterns, such as request-reply and publish-subscribe which assist you in creating and structuring your network.

Is ZeroMQ synchronous?

ZeroMQ is asynchronous brokerless signalling / messaging framework.

Does ZeroMQ use sockets?

ZeroMQ patterns are implemented by pairs of sockets with matching types. The built-in core ZeroMQ patterns are: Request-reply, which connects a set of clients to a set of services. This is a remote procedure call and task distribution pattern.


1 Answers

For connecting to the server, you need a DEALER socket on the client side, and a ROUTER socket on the server. Because you need a publish subscribe pattern you will need a SUB socket on the client side, and a PUB socket on the server side.

  Client       Server
+-------+      +--------+
| Dealer| <--> | Router |
|  Sub  | <--  |  Pub   |
+-------+      +--------+

So you bind the Router and the Pub sockets, and connect the Dealer, and the Sub sockets. Than when you want to:

  • Publish Messages (no reply expected): Create an envelope ( pub, channel, message ) and send it through the Dealer, on the Router side the router will receive the following envelope ( dealer, pub, channel, message ), so you can publish the message on channel through PUB socket.

  • Receive Messages (no reply expected): It's done with the SUB sockets on the client side, and since every publication goes through the ROUTER you can easily implement a subscription mechanism, or just add a SUB socket on the server side, and connect (inproc) to the PUB socket (maybe this is a cleaner solution).

  • Request / Receive Messages (reply expected): It can be done with the Dealer - Router. you just create a different envelope ( req, message ), so your Router ( receive: dealer, req, message ) will know that it should be processed, and can send a reply to the dealer. If your server needs to send requests to the clients, you just need to keep track of the connected clients, and send an envelope (dealer, req, msg), so your dealer can reply with example a ( rep, message ) envelope.

  • Streaming: as you stated it can be done with the publish pattern

This is how I would do it, if you need heart beating, it gets a bit complicated, but not much.

like image 182
balazs Avatar answered Oct 14 '22 06:10

balazs