I am using ZMQ NetMQ package in c# to receive the message from the subscriber. I am able to receive the msg but I am sticking in the while loop. I want to break the while loop if the publisher is stopped sending data.
Here is my subscriber code:
using (var subscriber = new SubscriberSocket())
{
subscriber.Connect("tcp://127.0.0.1:4000");
subscriber.Subscribe("A");
while (true)
{
var msg = subscriber.ReceiveFrameString();
Console.WriteLine(msg);
}
A :
There are at least two ways to do so :
a )
modify the code on both the
PUB-side andSUB-side, so that the Publisher sends both thePUB/SUB-channel messages, and independently of that alsoPUSH/PULL-keep-alive messages to prove to theSUB-side it is still alive, as being autonomously received as confirmations from thePULL-AccessPoint on theSUB-side loop. Not receiving such soft-keep-alive message for some time may trigger theSUB-side loop to become sure tobreak. The same principle may get served by a reversedPUSH/PULL-channel, whereSUB-side, from time to time, asks thePUB-side, listening on thePULL-side, using asynchronously sent soft-request message to inject a soft-keep-alive message into thePUB-channel ( remember the TOPIC-filter is a plain ASCII-filtering from the left to the right of the message-payload, soPUSH-delivered message could as easily send the exact text to be looped-back viaPUB/SUBback to the sender, matching the locally known TOPIC-filter maintained by the very sameSUB-side entity )b )
in cases, where you cannot modify the
PUB-side code, we still can setup a time-based counter, after expiring which, without receiving a single message ( be it using a loop of a known multiple of precisely timed-aSUB.poll( ... )-s, which allows for a few, priority-ordered interleaved control-loops to be operated without uncontrolled mutual blocking, or by using a straight, non-blocking form ofaSUB.recv( zmq.NOBLOCK )aligned within the loop with some busy-loop avoiding, CPU-relievingsleep()-s ). In case such timeout happens, having received no actual message so far, we can autonomously break theSUB-side loop, as requested above.Q.E.D.