Search code examples
c++zeromqpublish-subscribe

ZMQ: are messages published in extended pub-sub when there are no subscribers?


According to the ZMQ guide, when using a standard pub-sub pattern, if there are no connected clients the publisher does not publish any messages.

If a publisher has no connected subscribers, then it will simply drop all messages.

However, what happens if you are using an extended pub-sub pattern i.e. the publishers and subscribers are mediated by zmq_proxy with an xsub and xpub socket? It's difficult to infer what happens in this scenario if there are no subscribers. My assumption would be that the xsub socket acts as a subscriber and that all publishers would continue publishing irrespective of whether there were any actual subscribers connected to the proxy. But that is little more than an educated guess. Furthermore it's quite difficult to experimentally verify what happens in such a case - I imagine I'd have to use wireshark or something similar to monitor the network traffic. Thought I'd ask the question before I start down that road :)

Thanks in advance


Solution

  • The Proxy routes published messages from upstream producer (PUB) to downstream consumer (SUB). It passes subscribe/unsubscribe messages from the consumer to the publisher. The publisher's PUB socket drops any messages for which there is no subscription.

    So if there are no consumers downstream of the Proxy, and the Producer only publishes to the Proxy, all the producer's messages will be dropped by its PUB socket. Only after a consumer subscribes via the Proxy's XPUB (and the proxy, in turn, forwards the subscription to the producer via XSUB to PUB) will the messages actually be forwarded by the producer's PUB socket, through the proxy, to the consumer.

    I'm not sure whether the proxy actively manages the passed-through subscriptions or simply passes them through and lets the producer's PUB socket handle it. I think it's the latter. The proxy does buffer the messages to allow for consumer (or producer) outage/reconnect.