Sockets
ZeroMQ is a library of sockets that talk to each other directly: no server, nothing stored, and a pattern that decides who receives each message.
The diagram as text
- PUSH/PULL: a pipeline: Ventilator: PUSH binds; Worker 1 PULL, dials gets 1 and 4; Worker 2 PULL, dials gets 2; Worker 3 PULL, dials gets 3; Sink: PULL, binds reads them in turn
- PUB/SUB: a broadcast: Publisher: PUB, binds matches the filters; SUB, dials filter jobs.; SUB, dials filter prices; orders.new: no filter matches, dropped
- DEALER/ROUTER: request and reply: Responder: ROUTER, binds an identity per connection; Requester: DEALER dials; Requester: DEALER dials
- Ventilator: PUSH binds → Worker 1 PULL, dials gets 1 and 4: in turn
- Ventilator: PUSH binds → Worker 2 PULL, dials gets 2
- Ventilator: PUSH binds → Worker 3 PULL, dials gets 3
- Worker 1 PULL, dials gets 1 and 4 → Sink: PULL, binds reads them in turn
- Worker 2 PULL, dials gets 2 → Sink: PULL, binds reads them in turn
- Worker 3 PULL, dials gets 3 → Sink: PULL, binds reads them in turn
- Publisher: PUB, binds matches the filters → SUB, dials filter jobs.: jobs.done
- Publisher: PUB, binds matches the filters → SUB, dials filter prices: prices.eur
- Publisher: PUB, binds matches the filters → orders.new: no filter matches, dropped
- Requester: DEALER dials → Responder: ROUTER, binds an identity per connection: identity A
- Requester: DEALER dials → Responder: ROUTER, binds an identity per connection: identity B
Every box in the picture is a socket in some process. A message goes from one socket to another over a connection between them. Nothing keeps it after that, so a socket that is not connected at the time never sees it.
Which side listens and which side dials is independent of which way messages flow. The stable
part of a topology binds, and the parts that come and go dial it. In the pipeline the ventilator
and the sink bind, and the workers dial both. A ZmqEndpoint
states the side this service takes.
A PUSH socket deals its messages to its connected peers in turn, one message to one peer. A PULL
socket reads from all of its peers in turn, so no peer starves the others. This is
ZmqQueue.
A SUB socket sends its filters to the PUB it is connected to. The PUB compares the first frame of
each message with every filter by prefix and sends the message only where one matches. A message
no filter matches is dropped, and the publisher is not told. This is
ZmqFanout, where a subscription's name is its
filter.
A ROUTER socket gives each connection an identity. It puts the identity in front of every message
it receives, and it sends a message to the connection whose identity is the first frame. A DEALER
sends and receives with no identity frame. This is ZmqRpc,
and Frames draws the envelope.
A socket may have many peers, and order holds between one pair of sockets only. ruststream-zeromq
runs on tcp:// and ipc://. Another scheme is refused when the broker connects, before any
socket opens.
The patterns are ZeroMQ's own (pipeline, publish-subscribe, request-reply), and the zguide explains which side binds.