[zeromq-dev] [Fwd: Re: socket call back when using the zmq_loop]

Petitdemange Sébastien sebastien.petitdemange at esrf.fr
Fri Dec 18 21:27:20 CET 2009

Martin Sustrik a écrit :
> Hi Sebastien,
>>> The only case where you need multiple 0MQ sockets is when using 
>>> multiple
>>> messaging patters simultaneously from the same thread, say both
>>> request/reply and publish/subscribe.
>> That exactly what I planed to do. Use the filtering layer of the
>> zmq_socket and link that filtering to a specific callback. But maybe
>> It's not the better way to do that!
> If all you need is filtering why not do it in a straightforward way?
> zmq::socket_t s (ctx, ZMQ_SUB);
> s.connect ("tcp://server001:5555");
> s.setsockopt (ZMQ_SUBSCRIBE, "abc.*", 5);
> while (true) {
>     zmq::message_t msg;
>     s.recv (&msg);
>     process_message (msg);
> }
> Obviously, if you need a callback, just use function pointer instead 
> of hard-wired 'process_message' function in the above example.
>> So If I understand correctly the filtering should be done by the
>> application or use one thread per zmq socket and let the socket do the
>> filtering or use the low level zmq_poll and make my own loop.
> Nope. 0MQ should do the filtering. The question is why would you want 
> to use multiple sockets. If you want to receive messages from two 
> sources, just connect/bind the same socket twice:
> zmq::socket_t s (ctx, ZMQ_SUB);
> s.connect ("tcp://server001:5555");
> s.connect ("tcp://server002:5555");
> s.setsockopt (ZMQ_SUBSCRIBE, "abc.*", 5);
>  etc.
> Martin
Hi Martin,

First thanks a lot for your quick replay.

In fact what I want to do is more like that:

zmq::socket_t s1 (ctx, ZMQ_SUB);
zmq::socket_t s2 (ctx, ZMQ_SUB);

s1.connect ("tcp://server001:5555");
s1.setsockopt (ZMQ_SUBSCRIBE, "abc.*", 5);

s2.connect ("tcp://server002:5555");
s2.setsockopt (ZMQ_SUBSCRIBE, "def.*", 5);

and after that do a polling or what ever else by in a single thread...


More information about the zeromq-dev mailing list