[zeromq-dev] Pub/sub question (0MQ2)

Martin Sustrik sustrik at 250bpm.com
Fri Nov 6 22:11:19 CET 2009


Hi Christian,

>I've been trying to get pub/sub to work, and I'm afraid I don't see how I
>can publish to more than one listener: I wrote a listener process that does
>the following (on windows):
>
>
>
>    ctx = zmq_init (1, 1, 0);
>
>    s = zmq_socket (ctx, ZMQ_SUB);
>
>    rc = zmq_setsockopt (s, ZMQ_SUBSCRIBE , "*", 1);
>
>    rc = zmq_bind (s, "tcp://127.0.0.1:5555");
>
>
>
>A sender does this:
>
>
>
>    ctx = zmq_init (1, 1, 0);
>
>    s = zmq_socket (ctx, ZMQ_PUB);
>
>    rc = zmq_connect (s, "tcp://127.0.0.1:5555");
>
>                // then sends...
>
>
>
>which works. The problem is that when I run _two_ listeners, only one of
>them receives the messages. The other one only gets them after the first one
>has exited. This is not the behavior I expect of publish/subscribe: any node
>that subscribes to a topic should receive it, right?. Am I doing something
>wrong? Do I need to use multicast/pgm (apparently not an option under
>Windows)?

As far as I understand the problem you have is caused by having two
listeners bound to the port 5555. This should fail on the same box
(EADDRINUSE) as each TCP port can be used exclusively by a single
application. Still, it may work on two different boxes. However, even in
that case publisher would have to connect to _both_ subscribers (i.e.
call connect function twice).

Much better model is to bind publisher to a particulat port, say 5555 and
simply connect to that port from each subscriber.

Martin



More information about the zeromq-dev mailing list