Search code examples
mqttiot

MQTT: How to know when all retained messages have been received


After subscribing to an MQTT topic (that may include wildcards), is there a way to know for sure that all retained messages for the subscribed topics have been received?

(to be more accurate : all messages that were stored retained message at the broker at the moment of subscription)

If there is no direct way, is there any hack to know it (like sending a new retained message with specific QoS to a subscribed topic, that is for sure received after older messages) ?

Background of my question is this feature request (https://github.com/marvinroger/homie-esp8266/issues/313) that I want to implement.

So this would be the reference implementation for arduino (ESP8266) with the AsyncMQTT library, but my question is valid in general for other implementations, too (eg using paho-mqtt etc.).


Solution

  • Short answer, No.

    The client has no way of knowing what topics may have a retained message waiting on them at any point, let a lone at the time of subscribing.

    The only thing you can do is to make sure you subscribe at QOS 1 or 2 at which point the broker will make every effort to deliver them to you.

    The only indication would be the arrival of the first none retained message as the broker should (I think) deliver all the retained messages before any new ones. But given any update to that topic is likely to be flagged as retained as well that might not help, you would need a (none retained) message on a different topic to get a feel. Edit: messages on have the retained bit set when being delivered as part of a new subscription, later messages get the bit striped by the broker.