Waiting for a successful connection before sending

384 views
Skip to first unread message

Kerry Todyruik

unread,
Nov 6, 2015, 11:01:20 AM11/6/15
to netmq-dev
Hi,

Sorry if this question has been asked before. I'm new to ZeroMQ and NetMQ and was struggling to get a proxied pub/sub example working yesterday. I was referencing the xpub/xsub page in the documentation here: 

I discovered that my publisher worked when there was a Thread.Sleep after a Connect() which led me to this StackOverflow question about waiting for a connection before publishing messages:

What was happening before I added Sleep(), was it would create a new publisher socket and then immediately send several rapid messages which appeared to be lost. My subscriber never received them.

I have a few questions related to this. 

1. Am I correct that the issue is that the Connect() is asynchronous and I was sending messages too soon after connecting?

2. What is the best way to deal with this without using Thread.Sleep()? Can somebody provide an example or point me to an existing example somewhere?

3. Also, this only seemed to be a problem with using an intermediate xpub/xsub proxy. When I did a simpler 2 node pub/sub it worked fine. Why did my 2 node pub/sub work, but the 3 node xpub/xsub didn't? 

4. Is there anything I should be learning about to make my messaging implementation more reliable?

4. In the my non-working example, my messages were just lost and I didn't have any visibility in to where they were going. It was just through luck and randomly trying different things that I eventually added Thread.Sleep and got it working. How could I troubleshoot this better and get more feedback into what is happening? Are there events I could hook into or logging or something like that? 

Thanks for you help.
Kerry

Drew Noakes

unread,
Nov 6, 2015, 12:53:35 PM11/6/15
to Kerry Todyruik, netmq-dev
Can you have the subscriber send the publisher a message indicating it should publish, using another pair of sockets? You can pair pub+sub with sub+pub, giving fully bidirectional pub/sub between all peers.

I'd be interested in hearing other solutions.

--
You received this message because you are subscribed to the Google Groups "netmq-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netmq-dev+...@googlegroups.com.
To post to this group, send email to netm...@googlegroups.com.
Visit this group at http://groups.google.com/group/netmq-dev.
For more options, visit https://groups.google.com/d/optout.

Doron Somech

unread,
Nov 6, 2015, 3:39:55 PM11/6/15
to Kerry Todyruik, netmq-dev
Sleep should be used for testing only. The reason is that in real world scenario you don't know when the server will be up and when the client will try to connect, so client will miss all messages sent before it was up.

Pub-Sub out of the box is unreliable communication, like radio, if you just turn on the radio you are hearing what is live now, but missed everything before.
So as I see it there are two solutions to the pub-sub reliability problem, the first is the Clone pattern from the zeromq guide, read about it it will probably solve your problem. 
The Clone problem is good when you are sending state update, but if you need a solution for event updates this is what I use for CQRS/event sourcing system:

1. You need to have streams (topics), each stream should have a sequence number. I use the aggregate id as the stream name and the version of the aggregate as the sequence.
2. Before publishing save the events to a database, then publish. Save in bulk to improve performance.
3. Before subscribing make a query to the database to get all previous events (or last 100 or save the last event handled)

Anyway I only recommend this pattern if you really cannot use the clone pattern, clone is much simpler and I use it a lot.  




--

Mark Snelling

unread,
Nov 9, 2015, 4:19:14 AM11/9/15
to netmq-dev
Hi Doron,

Is there an example of the clone pattern implemented in NetMQ anywhere?

Thanks,

Mark

Doron Somech

unread,
Nov 10, 2015, 4:33:00 AM11/10/15
to Mark Snelling, netmq-dev
No that I know of. I might get to that soon. If you want to make it I can help
Reply all
Reply to author
Forward
0 new messages