Reputation: 2611
Could anyone please provide a real-world example of using zmq with the router/dealer pattern, and explain its advantage over the more simple publish/subscribe pattern? Thanks.
Upvotes: 12
Views: 7667
Reputation: 11736
Dealers send their client ID with each message. Router is able to accept connections from multiple dealers and each time a message is received, it is able to discern which client sent the message. It is also able to send messages to specific clients by referring to their IDs.
Real life example: game server lets clients join a room until it is full. It keeps track of each client ID in the room. When room is full, it loops over each client ID within that room and sends them a "game started" message.
Upvotes: 5
Reputation: 1530
A 'real-world' example: Stock market simulation
Router socket is the server (or 'Market'), Dealer sockets are the clients (or 'Traders').
This sort of behavior would be quite cumbersome to implement with pub/sub as you would need both Market and Traders to run a Publisher and a Subscriber socket to allow the 2 way communication. There would also be privacy concerns if all completed transactions were 'published' rather than sent direct to the relevant traders. (Trader B should not get to know that Trader A bought or sold something).
What makes Router sockets different
Sending and receiving from Router sockets are slightly more complicated to allow the asynchronous responses:
The 'identity' is a string and will be set to something unique per connected client by default, but you can set custom identities on client sockets via socket options.
Upvotes: 15