Session expiry
When a device connects to IoT Platform, a session is created. The session state includes subscribed topics, retained messages, last will messages, and Quality of Service (QoS) 1 messages cached by IoT Platform. A session expires and is deleted after a device disconnects from IoT Platform. This topic describes session expiry, a new feature in MQTT 5.0.
Background information
MQTT 3.1.1 does not have a concept of session expiry. Instead, the
cleanSessionflag determines whether to resume a previous session state when a device reconnects. A session is either purged when the device disconnects or is retained until the device reconnects to IoT Platform.cleanSession = true: A new session is created when the connection is established. The device must resubscribe to topics and will not receive any QoS 1 messages cached by IoT Platform.cleanSession = false: The previous session is resumed when the connection is established. The device does not need to resubscribe to topics and will receive QoS 1 messages cached by IoT Platform.
MQTT 5.0 introduces the concept of session expiry. It replaces
cleanSessionwithcleanStartand adds theSession Expiry Intervalproperty. These two parameters provide more granular session control than thecleanSessionflag in MQTT 3.1.1. They allow you to retain a session state for a period after a device disconnects or delete a session when it is no longer needed. This saves cloud resource consumption and prevents the device from receiving unexpected messages.
cleanStartis a binary flag. It indicates whether to start a new session and clear any existing session state information when a device reconnects to IoT Platform.cleanStart=true: The previous session is deleted when the connection is established. The device must resubscribe to topics and will not receive any QoS 1 messages cached by IoT Platform.cleanStart=false: If the session has not been deleted, the previous session is resumed. After the device disconnects, the session is retained for the duration of theSession Expiry Interval. If the device reconnects to IoT Platform within this interval, it does not need to resubscribe to topics and will receive QoS 1 messages cached by IoT Platform. If the device does not reconnect within this interval, the session is deleted. The device must then resubscribe to topics and will not receive any QoS 1 messages cached by IoT Platform.
Session Expiry Intervalis a time interval in seconds. IoT Platform retains the session state for this interval after a device disconnects. If this parameter is not set, the default interval is 0. If you set it to 0xFFFFFFFF, the interval is infinite.The combination of the
Session Expiry Intervalproperty and thecleanStartflag results in the following four cases:cleanStart=trueandSession Expiry Interval=0. The session is purged immediately when the device disconnects. The device must establish a new session when it reconnects.cleanStart=trueandSession Expiry Interval=∞. The result is the same as in the first case. IfcleanStart=true, the device establishes a new session when it reconnects, regardless of theSession Expiry Intervalvalue.cleanStart=falseandSession Expiry Interval>0but less than the disconnection interval. The session is retained for a short time. The session is deleted before the device reconnects to IoT Platform.cleanStart=falseandSession Expiry Interval=∞. The session is retained indefinitely. The device resumes the previous session when it reconnects.
Scenarios
Devices that frequently disconnect
For devices that are constantly moving, such as vehicles, frequently purging sessions can lead to frequent resubscriptions. This scenario corresponds to case 3 in the preceding figure. If the device reconnects within a specific period, the session is reused. If the device cannot connect for a long time, the session is purged. This prevents the device from receiving and processing expired messages.
Devices that cannot lose messages
For financial devices, such as point of sale (POS) machines and announcement speakers, the message delivery rate requirement is very high. This scenario corresponds to case 4 in the preceding figure.
The session remains consistent from creation to termination, ensuring that subscription relationships and messages are not lost.
Stateless devices
For devices such as advertising screens that continuously update and display new content, receiving expired messages is unnecessary. This scenario corresponds to case 1 in the preceding figure.
Not retaining sessions prevents the receipt of invalid, expired content and saves cloud resource consumption.