Closed jame40307 closed 1 year ago
This is quite possible; the MQTT spec says:
When Clients make subscriptions with Topic Filters that include wildcards, it is possible for a Client’s subscriptions to overlap so that a published message might match multiple filters. In this case the Server MUST deliver the message to the Client respecting the maximum QoS of all the matching subscriptions [MQTT-3.3.5-1]. In addition, the Server MAY deliver further copies of the message, one for each additional matching subscription and respecting the subscription’s QoS in each case.
So whether the broker sends one copy or two is not defined and what happend depends upon your broker (e.g. Mosquitto provides the option allow_duplicate_messages
, EMQX sends two messages). The v5 spec addresses this.
Unfortunately the client has no way of knowing this. All it knows is that it receives two messages, both of these match both subscriptions so it passes them to the callback.
The only ways around this that I'm aware of are:
SetDefaultPublishHandler
and set the callbacks to nil (e.g. client.Subscribe(topic, 1, nil)
) - the call back will still be called twice (but I guess that is better than four times!)Thank you for your response. It helps a lot in understanding where the issue lies.
Actually, I am providing a wrapper package that includes an MQTT module. I want it to offer different topic options with wildcards for a specific topic (e.g., for "item/10000001/change", users can subscribe with options like "item/#", "item/+/change", and so on). This means that I shouldn't prevent external systems from subscribing with overlapping topics. Some systems using my module may be interested in all items, while others may only be interested in specific item changes, and so on.
I think I will try the following approaches:
Great - I'm going to close this issue (as it's not really an issue with the library and it seems you have a solution).
I am trying to create a wildcard subscription for topic, it looks like:
When I publish a message to the topic "topic/test/1", I expect the messagePubHandler to be called twice. However, it is being called four times. This means that for each subscribed topic, the broker sends the message to the receive channel (2 times), and for each received message, it goes through all the message routes to find a matching handler (also 2 times). This results in a total of 2*2=4 calls.
I would like to know if this behavior is correct. If it is, is there a way to achieve the expected behavior?