0
votes

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!

1

1 Answers

1
votes

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