All Products
Search
Document Center

ApsaraMQ for MQTT:Session expiry interval

Last Updated:Aug 23, 2024

This topic describes the session expiry interval feature of Message Queuing Telemetry Transport (MQTT) 5.0.

Feature description

You can use the session expiry interval feature to specify the retention period of session status data after a session between the client and the broker ends. Session status data includes unacknowledged quality of service (QoS) 1 messages and client subscriptions. When a client connects to an MQTT broker, you can configure the Session Expiry Interval parameter in the CONNECT message to specify the session expiry interval.

Limits

  • If you do not configure the Session Expiry Interval parameter, the default value 0 is used, which specifies that the session expires immediately after disconnection. You cannot specify a retention period shorter than 10 minutes for the Session Expiry Interval parameter. If you specify a value smaller than 10 minutes, 10 minutes is used.

  • The maximum value of the Session Expiry Interval parameter is 2147483647.

  • If only point-to-point (P2P) messages are subscribed, the session expiration interval feature is not supported.

Usage notes

  • The following rules apply to the value of the Session Expiry Interval parameter (unit: seconds).

    • The value 0 specifies that the session expires immediately after the session ends.

    • A value greater than 0 specifies that the session is retained for the specified period of time.

Scenarios

  • Client reconnection: In an unstable or mobile environment, such as Internet of Vehicles (IoV), the client may be frequently disconnected. You can specify a longer session expiry interval to ensure that a session can be seamlessly recovered after being re-established.

  • Temporary device connection: You can specify a shorter session expiry interval for devices that temporarily connect to brokers or that disconnect from brokers after sending a small amount of data, such as a device used only once to report data or a sensor that temporarily joins a network. This helps release broker resources and prevent idle sessions from occupying storage space.

  • Security and privacy protection: For applications that have high data confidentiality and privacy requirements, you can specify a session expiry interval based on your business requirements to ensure that session status data is automatically cleared after the client remains inactive for a long period of time. This reduces the risk of data leakage.

The session expiry interval feature of MQTT 5.0 allows you to flexibly change session expiry intervals based on the application scenario to ensure optimal system performance, high resource utilization, and excellent user experience.

Example

An Internet of Things (IoT) device needs to frequently disconnect from and reconnect to the MQTT broker. To prevent subscriptions and messages from being lost after the device is disconnected, use the session expiry interval feature of MQTT 5.0.

The following section describes how to use the feature during the first connection and subsequent reconnections of the device.

First connection

The first time the device connects to the MQTT broker, a CONNECT message is sent to the broker and the Session Expiry Interval parameter is set to 4 hours. This value specifies that session status data, including subscribed messages and messages in queues, is retained for up to 4 hours after the device is disconnected.

CONNECT
ClientId: IoT_Device123
Session Expiry Interval: 14400 // Specifies that the session expiry interval is 4 hours. Unit: seconds.
Clean Start: 0 // Specifies that the broker continues to use the existing session.
// Other required information in the CONNECT packet.

If the device is disconnected from the MQTT broker due to network issues within the 4-hour retention period, the broker retains all subscribed and unacknowledged messages in the device.

Reconnection

CONNECT
ClientId: IoT_Device123
Session Expiry Interval: 14400 // Specifies that the session expiry interval is still 4 hours.
Clean Start: 0 // Notifies the broker to use previously retained session status data.
// Other required information in the CONNECT packet.

When the IoT device reconnects to the MQTT broker and session is not expired, the broker uses the previously retained session status data. In this case, messages that are subscribed and not received by the device are sent to the device after reconnection.

Timeout

If the IoT device is not reconnected to the MQTT broker after 4 hours, the broker clears the previously retained session status data. After the device is reconnected, you must establish a new session and subscribe to the required topics for the IoT device. In this case, messages in the previous queues are lost.