socketsmessage-queuezeromqpyzmqnetmq

How to pause PUSH messages until the socket has a client with ZeroMQ?


I am using ZeroMQ and NetMQ to allow for a two-way communication between some python scripts and a Unity C# script. I implemented it with two PUSH-PULL sockets, one for each way. This works fine. The problem I now have is that I want to stop or pause sending messages from the python server when there is nobody pulling these messages.

I read that ZeroMQ does not allow any native API calls to manage the Queue or something like that, but I could try setting the ZMQ_SNDHWM to something like 1. The problem is that I don't care about older state and the push socket would mute any outgoing messages if the high water mark is met.

When a ZMQ_PUSH socket enters the mute state due to having reached the high water mark for all downstream nodes, or if there are no downstream nodes at all, then any zmq_send operations on the socket shall block until the mute state ends or at least one downstream node becomes available for sending; messages are not discarded.Is there a way to detect if someone is pulling the messages or if someone is connected to the socket and otherwise pause or flush the queue?
~ https://libzmq.readthedocs.io/en/zeromq3-x/zmq_socket.html

Thanks in advance.

controls_socket = zmq.Context().socket(zmq.PUSH)
controls_socket.bind("tcp://127.0.0.1:5556")

while is_running:
  [...]
  controls_socket.send_string(control_commands)

Solution

  • You could use the ZMQ_CONFLATE (zmq.CONFLATE) socket option. This ignores the HWMs and only keeps the last/latest message sent/received in the buffer.

    NOTE: