Retained messages ensure that new subscribers to a topic receive the latest status message, even if the message was published before they subscribed. When the server publishes a message with the retain flag to a topic, IoT Platform saves the message and delivers it to current subscribers. Devices that subsequently subscribe to the topic also receive the retained message. Retained messages are defined in the MQTT 3.1.1 standard. Alibaba Cloud IoT Platform supports this feature for MQTT 5.0 connections.
Retained messages
-
Update a retained message:
-
Publishing a new message to the same topic overwrites the payload of the existing retained message.
-
The retain flag can be used to publish messages to a batch of devices, regardless of their online status.
-
-
Delete a retained message:
-
Publishing a message with an empty payload to the same topic deletes the retained message.
-
A retained message is deleted after its message expiration interval passes. This feature is useful for messages that become invalid after a certain period, such as temperature control data. To use this feature, set the message expiration attribute.
-
A retained message is not affected by session expiration and persists even if the session expires.
-
Scenarios
Retained messages are ideal for configuring offline devices. For example, an advertising screen hibernates at night to save energy and turns on during the day. Its content is updated daily. Pushing updates from the server is more efficient than having the device poll for changes. However, if the server pushes an update while the device is offline, the message cannot be delivered. Retained messages solve this problem by ensuring that the device receives the latest message the next time it comes online.