Search code examples
zeromq

Retrieving subscriber count using zeromq PUB/SUB sockets


Is it possible to get the total count of subscribers from a PUB socket in zeromq?

Thanks!


Solution

  • Yes, but unfortunately not via any simple property or method.

    You need to use the zmq_socket_monitor() function to connect an inproc service socket to the main socket you want to observe. From there you can listen to events regarding connect/disconnect and keep your own count of subscribers. It may not be a trivial task though, since it seems (to me at least) a bit hard to know when to consider a subscriber (or any remote connection) to be up/down (closed/disconnected/retry etc.). You will have to play around a bit.

    The link includes samples and event descriptions.