Receive offline messages
MQTT clients on unstable networks or resource-constrained devices can disconnect at any time. When a client is offline, messages published to its subscribed topics are queued as offline messages. After the client reconnects and passes permission verification, ApsaraMQ for MQTT automatically delivers these queued messages. No manual pulling is required.
How offline messages work
The client disconnects from the ApsaraMQ for MQTT broker.
Messages published to the client's subscribed topics are queued as offline messages.
The client reconnects to the broker and passes permission verification.
ApsaraMQ for MQTT automatically delivers the queued offline messages.
Delivery latency and batching
| Behavior | Details |
|---|---|
| Generation latency | A message is classified as offline only after the client's acknowledgment times out. If the client briefly disconnects and reconnects, the latest offline messages may not be available immediately. Expect a latency of 5 to 10 seconds. |
| Batch delivery | When more than 30 offline messages are queued, they are delivered in batches of 30 at 5-second intervals. |
Prerequisites
Before you begin, make sure that you have:
Permission verification set up for the client -- the client must pass permission verification after connecting before offline messages are loaded. If you use token-based verification, upload the token and complete verification first.
Configure offline message delivery
Set the Quality of Service (QoS) level and the cleanSession parameter on the consumer to control offline message delivery.
The cleanSession parameter
| Value | Behavior |
|---|---|
cleanSession=true | The broker discards all previous session state when the client connects. Subscriptions and queued messages from prior sessions are removed. The client starts with a clean session each time. |
cleanSession=false | The broker preserves session state across disconnections. Subscriptions remain active and offline messages are queued for delivery when the client reconnects. |
To receive offline messages, set cleanSession to false.
QoS levels
MQTT defines three QoS levels that control message delivery guarantees:
| QoS level | Delivery guarantee | Description |
|---|---|---|
| QoS 0 | At most once | Fire-and-forget. The message is sent once with no confirmation. |
| QoS 1 | At least once | The sender retries until the receiver acknowledges. Duplicates are possible. |
| QoS 2 | Exactly once | A four-step handshake guarantees the message arrives exactly once. |
QoS and cleanSession behavior matrix
The following table shows how each combination of QoS level and cleanSession value affects offline message delivery.
| QoS level | cleanSession=true | cleanSession=false |
|---|---|---|
| QoS 0 (at most once) | Offline messages are not delivered. Only one delivery attempt is made for online messages. | Offline messages are delivered. Only one delivery attempt is made for online messages. |
| QoS 1 (at least once) | Offline messages are not delivered. Online messages are guaranteed to reach the client. | Offline messages are delivered. Both offline and online messages are guaranteed to reach the client. |
| QoS 2 (exactly once) | Offline messages are not delivered. Online messages are guaranteed to reach the client exactly once. | Not supported. |
Choose the right configuration
| Use case | Recommended configuration |
|---|---|
| Publish-only client that does not need offline messages | QoS 0 or QoS 1, cleanSession=true |
| Client that must receive all missed messages after reconnection | QoS 1, cleanSession=false (recommended) |
| Client that needs exactly-once delivery for online messages only | QoS 2, cleanSession=true |
For more information about the QoS and cleanSession parameters, see Terms.