zero mq pub/sub with multipart not working
Credit: Chuck Remes
You may need a "sleep" between the socket creation steps (bind, connect, setsockopt) and the actual transmission of the messages. The bind & connect operations are asynchronous, so they may not complete by the time you get to the logic that sends all of the messages. In that case, any messages sent through the PUB socket will be dropped since a zmq_bind() operation does not create a queue until another socket has successfully connected to it.
As a side note, you don't need to create 2 contexts in this example. Both sockets can be created within the same context. It doesn't hurt, but it also isn't necessary.
Credit: Pieter
There is a "problem solver" at the end of Ch1 that explains this.
Some socket types (ROUTER and PUB) will silently drop messages for which they have no recipients. Connecting is, as Chuck said, asynchronous and takes approx 100msec. If you start two threads, bind one side, connect the other, and then start immediately to send data over such a socket type, you'll lose the first 100msec of data (approximately).
Doing a sleep is a brutal "prove that it works" option. Realistically you'd synchronize in some way, or (more typically) expect message loss as part of normal startup (i.e. see the published data as a pure broadcast with no definite start or end).
See weather update example, syncpub and syncsub for details.
Necro-posting, but for those interested in a solution other than sleeping, there are monitors.
You can set a monitor callback and get called on ZMQ_EVENT_CONNECTED events.
See details and example at http://api.zeromq.org/3-3:zmq-ctx-set-monitor.