I'm new to ZeroMQ. Today I am trying the pub/sub pattern with NetMQ (the ZMQ library for .NET). I noticed some strange behavior (at least, to me!). I create a subscriber socket and subscribes to topic "12345". The publisher then publishes messages with topic "1234567890". The subscriber can receive the messages! That means, the filter does not compare the whole topic string, but only checks if the published topic "starts with" the subscribed topic. To confirm that, I changed the subscribed topic to "2345". And the subscriber did not receive the messages. If I change the publishing topic to "23456890" (while the subscribed topic is "2345"), then the messages come! I'd like to know, is that the normal behavior of the topic filter in ZeroMQ (and NetMQ)? Thank you very much!
ZeroMQ pub sub filtering behavior
595 Views Asked by Kenna At
2
There are 2 best solutions below
0
On
Establish a new message filter. Newly created Subsriber sockets will filtered out all incoming messages. Call this method to subscribe to messages beginning with the given prefix. Multiple filters may be attached to a single socket, in which case a message shall be accepted if it matches at least one filter. Subscribing without any filters shall subscribe to all incoming messages. const sub = new Subscriber()
// Listen to all messages beginning with 'foo'. sub.subscribe("foo")
// Listen to all incoming messages. sub.subscribe() Params: prefixes – The prefixes of messages to subscribe to.
subscribe(...prefixes: Array<Buffer | string>): void;
Yes, this is documented property of ZeroMQ implementation of ultra-fast TOPIC-filtering. It works this way since ever ( v2.1.1+, v3.+, v4.+ and most probably it will remain so due to its ultimate performance and scaling envelopes ).
Also be informed, that a similar approach was taken by Martin SUSTRIK, the ZeroMQ co-father, in foundations of
nanomsgand its ports and more recent breeds (pynano,pynnget al ), so it could be called a de-facto industry best-practice, could it not?