The ZeroMQ documentation states that this is a problem with the PUSH / PULL settings and offers the following pattern: adding a REP / REQ setting to ensure node coordination when you expect a fixed number of subscribers. However, if you cannot know in advance the number of subscribers, you should consider changing your protocol in order to be more resistant to these conditions.
Synchronized Publisher in C (from ZGuide)
// // Synchronized publisher // #include "zhelpers.h" // We wait for 10 subscribers #define SUBSCRIBERS_EXPECTED 10 int main (void) { s_version_assert (2, 1); void *context = zmq_init (1); // Socket to talk to clients void *publisher = zmq_socket (context, ZMQ_PUB); zmq_bind (publisher, "tcp://*:5561"); // Socket to receive signals void *syncservice = zmq_socket (context, ZMQ_REP); zmq_bind (syncservice, "tcp://*:5562"); // Get synchronization from subscribers int subscribers = 0; while (subscribers < SUBSCRIBERS_EXPECTED) { // - wait for synchronization request char *string = s_recv (syncservice); free (string); // - send synchronization reply s_send (syncservice, ""); subscribers++; } // Now broadcast exactly 1M updates followed by END int update_nbr; for (update_nbr = 0; update_nbr < 1000000; update_nbr++) s_send (publisher, "Rhubarb"); s_send (publisher, "END"); zmq_close (publisher); zmq_close (syncservice); zmq_term (context); return 0; }
user7116
source share