2

I am a newcomer to zeromq. Recently I did some tests on pub/sub of zeromq, and I don't konw how to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ. Hope your help, thank you very much .

user2668079
  • 23
  • 1
  • 6

2 Answers2

2

Learn the basics by working through the examples. For the proxy, just use this, it's from msgqueue.cpp

int main (int argc, char *argv[])
{
    zmq::context_t context(1);
    zmq::socket_t frontend (context, ZMQ_XSUB);
    frontend.bind("tcp://*:5559");
    zmq::socket_t backend (context, ZMQ_XPUB);
    zmq_bind (backend, "tcp://*:5560");
    zmq_proxy (frontend, backend, NULL);
    return 0;
}
raffian
  • 31,267
  • 26
  • 103
  • 174
  • Thank you so much. But I also found anther problem. I started subscriber first, and then proxy, then publisher. In the publisher side, I sent 5000 * 2 KB messages, but in the subscriber side, I just received 951 * 2 KB messages. I wonder if this has something to do with "high-water mark" ? and what can I do for it ? Thanks. – user2668079 Sep 05 '13 at 14:23
  • That's a common problem, known as "slow subscriber". Even though you start subscriber first, some messages can be lost. You can avoid dropping messages by syncing subscriber with publisher before it sends, see this: https://github.com/imatix/zguide/blob/master/examples/C%2B%2B/syncsub.cpp and this https://github.com/imatix/zguide/blob/master/examples/C%2B%2B/syncpub.cpp – raffian Sep 05 '13 at 14:32
  • Yes, I did something to mark every message, and I found that what subscriber lost are the messages after 954, actually publisher have sent 5000 messages – user2668079 Sep 06 '13 at 05:12
  • I mean I have written "sleep()" some seconds in the publisher before it publishes messages – user2668079 Sep 06 '13 at 05:28
  • Just check the updated api, I got that example from zmq's own examples. – raffian Sep 06 '13 at 12:55
  • In the zmq v3.2, zmq.h, says, zmq_device() is the deprecated method. and messages still lost even I used zmq_device(). – user2668079 Sep 06 '13 at 13:29
  • It seems to be useless. – user2668079 Sep 07 '13 at 06:57
  • I have done as you say. I started subscriber first, and then proxy, then publisher. In the publisher side, I sent 5000 * 2 KB messages, but in the subscriber side, I just received 951 * 2 KB messages. what subscriber lost are the messages after 951 , but actually publisher have sent 5000 messages . – user2668079 Sep 11 '13 at 03:45
  • Is there any API to unbind the socket and destroy the context in the zmq namespace? I am seeing only zmq_unbind, zmq_close etc. functions from the zmq_... series. – RKum Feb 27 '18 at 15:15
  • @RupeshKumar I believe those are the only ones – raffian Feb 27 '18 at 16:17
  • 1
    What's the difference between `frontend.bind(...)` and `zmq_bind(backend,...)`? – ErniBrown Apr 03 '19 at 09:18
-2

Proxy:

int main (int argc, char *argv[]) {
    zmq::context_t context(1);
    zmq::socket_t frontend (context, ZMQ_XSUB);
    
    //set hwm...
    frontend.bind("tcp://*:5559");
    zmq::socket_t backend (context, ZMQ_XPUB);
    
    //set hwm...
    zmq_bind (backend, "tcp://*:5560");
    zmq_proxy (frontend, backend, NULL);
    return 0;
}

The reason I lost message is that I should have called setsockopt before bind or connect.

Refer to 0MQ API documentation for setsockopt:

Caution: All options, with the exception of ZMQ_SUBSCRIBE, ZMQ_UNSUBSCRIBE and ZMQ_LINGER, only take effect for subsequent socket bind/connects.

Federico Fusco
  • 545
  • 1
  • 5
  • 18
user2668079
  • 23
  • 1
  • 6