moquette icon indicating copy to clipboard operation
moquette copied to clipboard

Support Retained Message for QoS=0 with a config option

Open BigGitWorld opened this issue 3 years ago • 5 comments

Hi. I'm going to use save last state of some topics with the retained message concept. So my question is : Does the Moquette version 0.12.1 support Retained Message? and if the answer is yes, Can you tell me what class is used for this purpose? Thank you.

BigGitWorld avatar Dec 05 '20 15:12 BigGitWorld

Yes, it support since old versions.

Here is, for example, one of sides where the broker stores the retained: https://github.com/moquette-io/moquette/blob/d8a59f135b2cf0896e04f6f02483b0841920fe60/broker/src/main/java/io/moquette/broker/PostOffice.java#L195-L202

Look at implementation of IRetainedRepository: https://github.com/moquette-io/moquette/blob/d8a59f135b2cf0896e04f6f02483b0841920fe60/broker/src/main/java/io/moquette/broker/IRetainedRepository.java#L27

andsel avatar Dec 06 '20 10:12 andsel

Thank you @andsel I checked the source code and see the following code in the PostOffice for a published message with QoS=0 :

void receivedPublishQos0(Topic topic, String username, String clientID, ByteBuf payload, boolean retain,
                             MqttPublishMessage msg) {
        ...
        if (retain) {
            // QoS == 0 && retain => clean old retained
            retainedRepository.cleanRetained(topic);
        }
        ...
    }

Why do you clean retained message with QoS=0?

I think its better to add a config option to let developers to choose one of the below different behaviors :

  • If they want to behave like as QoS=1,2 and save retained message with QoS=0 also.
  • If they want to clean previously saved message in the broker, they have to publish a retained message with an empty payload (an empty string)

what do you think about this config option?

BigGitWorld avatar Dec 06 '20 13:12 BigGitWorld

This come from http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html normative [MQTT-3.3.1-7] which states:

If the Server receives a QoS 0 message with the RETAIN flag set to 1 it MUST discard any message previously retained for that topic. It SHOULD store the new QoS 0 message as the new retained message for that topic, but MAY choose to discard it at any time - if this happens there will be no retained message for that topic.

andsel avatar Dec 08 '20 15:12 andsel

I know it. But I changed that behavior to be same as QoS=1,2 in the Moquette broker. In fact almost all of my IoT devices are using QoS=0 and don't want to change their QoS. So I changed the broker behavior to save messages for all QoS values if the retained flag is set to true.

BigGitWorld avatar Dec 10 '20 08:12 BigGitWorld

I feel your pain, but fixing this with the flag on Moquette broker make me rise some questions:

  • patching the broker to go out of specs instead of client, seems like an antipattern
  • seems to patch for only specific use case

andsel avatar Dec 10 '20 08:12 andsel