Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain the request-reply broker zeromq example?

Tags:

zeromq

broker

I'm refering to the 'A Request-Reply Broker' in the Zeromq documentation: http://zguide.zeromq.org/chapter:all

I'm getting the general gist of the app: it acts like an intermediary and routes messages from the client to the server and back again.

What I'm not getting though is how it makes sure the correct response from a server is sent to the correct client which originally made the request. I don't see anything in the code example which makes sure about this.

Now in the example they only send 1 message (hello) and 1 response (world), so even if messages are mixed up it doesn't matter, but I'm guessing that the testclient and server are kept deliberately simple.

Any thoughts are welcome...

like image 649
Toad Avatar asked Dec 06 '10 20:12

Toad


People also ask

Is ZeroMQ async?

It will allow async messaging, but will store messages if a endpoint drops away for a time. For ordering, ZeroMQ is a abstraction of a FIFO queue and is built over TCP.

How do I know if my Zmq socket is connected?

No, there's no method in the API to check if a socket is connected. ZeroMq abstracts the network; client and server connections are completely transparent to the peer making the connection.

What is Rep socket?

REP sockets are synchronous and talk to one peer at a time. If you connect a REP socket to multiple peers, requests are read from peers in fair fashion, and replies are always sent to the same peer that made the last request.


2 Answers

All zeromq sockets implicitly have an identity associated with them. (You can obtain this identity with zmq_getsockopt().)

For bi-directional socket types not XREQ or XREP, this identity is automatically transferred as part of every message sent over the socket. The REP socket uses this identity to route the response message back to the appropriate socket. This has the effect of automatic routing.

Under the hood, identities are transferred via multipart messages. The first message in a multipart message will contain the socket identity. An empty message will follow, followed by all messages specified by the user. The REQ and REP sockets deal with these prefixed messages automatically. However, if you are using XREQ or XREP sockets, you need to populate these identity messages yourself.

If you search for "identity" on the ZMQ Guide, you should find all the details you will ever want to know about how identities and socket routing works.

like image 199
IndyGreg Avatar answered Oct 10 '22 10:10

IndyGreg


Ok in chapter 3 they all of a sudden explain that there is an underlying concept of an 'envelope' which the req/resp pattern invisubly uses.

This explains how it works.

like image 25
Toad Avatar answered Oct 10 '22 09:10

Toad