Receive offline messages

Updated at:
Copy as MD

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

  1. The client disconnects from the ApsaraMQ for MQTT broker.

  2. Messages published to the client's subscribed topics are queued as offline messages.

  3. The client reconnects to the broker and passes permission verification.

  4. ApsaraMQ for MQTT automatically delivers the queued offline messages.

Note The automatic delivery mechanism takes effect even if the client uses the message pulling method.

Delivery latency and batching

BehaviorDetails
Generation latencyA 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 deliveryWhen 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

ValueBehavior
cleanSession=trueThe 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=falseThe 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 levelDelivery guaranteeDescription
QoS 0At most onceFire-and-forget. The message is sent once with no confirmation.
QoS 1At least onceThe sender retries until the receiver acknowledges. Duplicates are possible.
QoS 2Exactly onceA 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 levelcleanSession=truecleanSession=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 caseRecommended configuration
Publish-only client that does not need offline messagesQoS 0 or QoS 1, cleanSession=true
Client that must receive all missed messages after reconnectionQoS 1, cleanSession=false (recommended)
Client that needs exactly-once delivery for online messages onlyQoS 2, cleanSession=true

For more information about the QoS and cleanSession parameters, see Terms.