Retain

MQTT does not define a mechanism for the publisher of a message to be guaranteed that a subscriber actually received the message. The publisher can only use the QoS level to ensure that the message was delivered securely to the message broker. On the other hand, even a subscriber cannot ensure when a publisher has sent a message. In the worst case, the recipient subscribes to the topic only after the publisher has sent a message. In this case, the subscriber would not receive the message. If the retain flag is set on publish, the subscriber would receive it. The following diagram illustrates this relationship once again:

Retain 1:

The so-called retain flag offers the possibility during the publish process to inform the message broker that it will persistently retain the message in the corresponding topic. The message broker always saves the last retain message sent. Any client that subscribes to this topic (either before or after publish) will then get this message delivered immediately once they set up the subscription. This process also works for Wildcard Subscriptions.

A retain message can also be removed from a topic by sending a message without payload to the corresponding topic with the retain flag set.