eclipse / paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
2.12k stars 884 forks source link

Callbacks not invoked for shared-subscriptions #367

Open codeasone opened 7 years ago

codeasone commented 7 years ago

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...

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.

yogin16 commented 7 years ago

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

jpwsutton commented 7 years ago

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.

yogin16 commented 7 years ago

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

icraggs commented 5 years ago

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.

vborcea commented 5 years ago

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

icraggs commented 5 years ago

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.

pschichtel commented 4 years ago

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

pschichtel commented 4 years ago

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

yuremboo commented 10 months ago

Is there any news about when it will be released?

nwest1 commented 7 months ago

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