I'm experiencing unexpected behavior with MQTT shared subscriptions. Here's the scenario:
I have two MQTT subscribers:
When I publish a message to the topic "1234/test/A", both subscribers receive the message.
However, when I change both subscriptions to "$share/group/1234/#", the behavior is as expected: only one subscriber receives each message.
I've tested this behavior on various public MQTT brokers, and the results are consistent across all of them.
This seems to contradict the expected behavior of shared subscriptions, where messages should be distributed among subscribers in a shared group.
Any insights or suggestions for resolving this issue would be greatly appreciated.
The MQTT V5 spec states:
If a Server receives a Topic Filter that is identical to the Topic Filter for a Shared Subscription that already exists on the Server, the Session is added as a subscriber to that Shared Subscription. No retained messages are sent.
Note that this says the Topic Filter must be identical, not that the Topic Filters overlap. For completeness, the Topic Filter described as:
A Shared Subscription is identified using a special style of Topic Filter. The format of this filter is:
$share/{ShareName}/{filter}
So having an identical ShareName
does not mean that the subscriptions are shared, the entire topic filter must be identical. $share/group/1234/test/A
and $share/group/1234/#
do not have identical topic filters, so are not the same shared subscription (and what you are seeing is to be expected).
In your step 3, where both clients subscribe to $share/group/1234/#
the topic filters are identical, so the second subscriber is added to the shared subscription (and things work as you expect them to).