paho.mqtt.java icon indicating copy to clipboard operation
paho.mqtt.java copied to clipboard

Callbacks not invoked for shared-subscriptions

Open codeasone opened this issue 8 years ago • 10 comments

Callbacks don't get called when subscribing to shared subscriptions, e.g. $share/group/topic, using the subscribe(String topicFilter, int qos, IMqttMessageListener messageListener)API.

However, shared subscriptions work as expected when using the setCallback(...) API before invoking subscribe(String topicFilter, int qos).

I have written a simple Java application which reproduces the issue.

https://github.com/codeasone/shared-subscriptions-issue

This issue impacts wrappers such as the Clojure library machine-head, which is what led me here...

  • [ ✓] Bug exists Release Version 1.1.1 ( Master Branch)
  • [ ?] Bug exists in Snapshot Version 1.1.2-SNAPSHOT (Develop Branch)

HEAD of develop branch throws exceptions with my test program suggesting a regression wrt. 1.1.x API, which may or may not be related.

codeasone avatar May 04 '17 10:05 codeasone

Great explanation in the issue. We are also facing similar issue.

Upon checking further I found out that in CommsCallback's deliverMessage it does exact topicName matching with filter as per the standard. And as $share or $queue is not defined in MQTT standard - it is not matching. If the message hasn't been delivered to a per subscription handler, it would give it to the default handler - as it is working using setCallback(...)

Should I go ahead and add support for external topic matcher in CommsCallback? If no external topic matcher registered then default to MqttTopic.isMatched(topicFilter, topicName)

The issue was notified in emqttd broker as well: https://github.com/emqtt/emqttd/issues/921

yogin16 avatar May 08 '17 14:05 yogin16

As @yogin16 says, Shared Subscriptions aren't in the MQTT 3.1.1 specification, there are some guidelines on topics starting with $ though (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718108). I'm hesitant about adding things that would take us off specification as it feels like a bit of a slippery slope. MQTTv5 will include shared subscriptions and we will definitely be implementing them in the Paho Java v5 client. Whether we add it to the 3.1.1 client though is something that I think should be discussed with the wider Paho community.

jpwsutton avatar May 09 '17 08:05 jpwsutton

I was able to solve this by adding a MqttCallback implementation for custom routing of unmatched topic filter.

Here is the example: https://github.com/yogin16/paho-shared-sub-example/blob/master/example/src/src/SharedSubCallbackRouter.java

yogin16 avatar Jul 09 '17 11:07 yogin16

We could have a method on the client (for MQTT V3 - MQTT V5 has it in the spec), which gives a list of prefixes (in regex format?), which are ignored in topic matching. This would cater for multiple different implementations.

icraggs avatar Nov 22 '18 15:11 icraggs

Hello, Is there an "official" solution for this?

vborcea avatar Jan 28 '19 09:01 vborcea

I like my idea above of specifying the prefixes to ignore in a method. Should be pretty easy to implement. I'll do that soon if no-one else does.

icraggs avatar Jan 28 '19 16:01 icraggs

Is this issue still present with the MQTT 5 client? It seems I'm facing the exact same issue.

pschichtel avatar Aug 16 '20 15:08 pschichtel

After looking at the code, I'd say the following few lines should use the topic with the $share/{id} prefix stripped:

https://github.com/eclipse/paho.mqtt.java/blob/master/org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/MqttAsyncClient.java#L1301-L1305

pschichtel avatar Aug 16 '20 16:08 pschichtel

Is there any news about when it will be released?

yuremboo avatar Nov 10 '23 12:11 yuremboo

Submitted this fix awhile ago, but would love to see some feedback on it - https://github.com/eclipse/paho.mqtt.java/pull/911

nwest1 avatar Feb 14 '24 16:02 nwest1