The benefits of MQTT Retain messages are that if any of the Subscribers subscribe to the topic, it will subscribe to the last updated payload, although when Publisher is not publishing the payload on the same topic continuously. So that if the payload is retained and any Subscriber subscribes to the same topic, it will get the last updated payload.
Examples of MQTT Retained messages.
For example — A Temperature sensor connected to say ‘ESP32’ microcontroller and ESP32 works as an MQTT Client and Publishing the sensors values every hour to MQTT Broker on Topic-’LM35’ with Retain flag. If another MQTT Client says ‘MqttDesk’ Subscribe the same topic in between the publishing hour time then it will get the sensor value which is already retained on Topic -‘LM35’.
Follow the below video to know about the MQTT Retained messages, how to clear them, & How they work?
MQTT QoS(Quality of Service) makes no impact on MQTT Retained messages.
How to clear retain flag from the topic?
We can publish a zero payload to the ‘LM35’ topic and this zero payload will clear the Retain flag from the topic from MQTT Broker. So now when ‘MqttDesk’ subscribes to the ‘LM35’ Topic, it won’t get anything but the payload next time when ‘ESP32’ Publish to the topic.