0

In reference to this article I think I know the difference between QoS1 and QoS2 messages, but I don't know the difference in handling both of them as a Paho MQTT client.

Imagine I subscribe to the topic like this:

MqttClient subscriber = new MqttClient(SERVER_URI, SUBSCRIBER_ID);
subscriber.subscribe(TOPIC);

And then I'm publishing messages like this:

publisher.publish(TOPIC, PAYLOAD, 1, false);

At this moment I'm using MqttCallback interface to handle messages that arrives to subscribers.

There is a method to override:

public void messageArrived(String topic, MqttMessage mqttMessage) {
    if(mqttMessage.isDuplicate()) {
        // is it really the duplicate message from my perspective?
    } else {...}
}

In the MqttMessage we can find a isDuplicate() method, but how can I be sure that the mqttMessage that returns true is not the first message that my subscriber received?

I am very interested in finding a solution that shows how to handle QoS1, but every answer which will clarify anything here will be appreciated.

Best regards from Cracow!

Witek
  • 3
  • 1
  • 4

1 Answers1

2

It's not sufficient to rely on the duplicate flag, since you could have missed the first message. If the QoS 1 messages are not idempotent, here are a few suggestions how you could do duplicate detection:

  • Hash the payload + topic and have a table with the last X messages and their hashes available so you can check if you already received that message
  • Have a unique ID in the payload and have a table with the last X messages and their ID available
  • Have a timestamp in the payload and have a table with the last X messages and their timestamp available

If you really need to make sure that a message arrives once and only once, you can use QoS 2. QoS 1 means that your clients can handle duplicates (either by ignoring a duplicate message or the messages are idempotent).

Dominik Obermaier
  • 5,610
  • 4
  • 34
  • 45
  • I see, I was hoping that there is an easier way to discover duplicated messages. Thank you for your quick answer. – Witek Jul 15 '15 at 11:37
  • Unfortunately I don't know of an easier way. What you could also do is using the MQTT message ID for comparing if you have already seen that message. This would be highly broker dependant, though since message Ids can be reused at any point of time (if a message flow is already finished) and they are not guaranteed to be e.g. sequentially. Most implementations provide sequentially message ids, though. – Dominik Obermaier Jul 15 '15 at 12:12