In MQTT , a persistent session refers to a feature that allows a subscribing client to maintain its session state and subscriptions with the MQTT broker. It ensures that the subscribing client does not lose important information or subscriptions when it disconnects and reconnects to the MQTT broker.
When an unexpected disconnection occurs, a subscribing client with a persistent session enabled will receive all published messages at QoS 1 and QoS 2 levels and then a retained message for the corresponding topic, upon reconnecting to the MQTT broker.
Clean Session Flag
The persistent session is set by using clean session flag when a subscribing client connects to a MQTT broker.
Clean Session Flag | Persistent Session |
True | False |
False | True |
The behavior of the MQTT session is determined by the "clean session" flag set in the CONNECT message when the client establishes a connection with the broker.
-
Clean Session Flag set to true: If the client sets the clean session flag to true, it indicates that it does not want a persistent session. In this case, when the client disconnects from the broker, all session-related information, including subscriptions and queued messages, are discarded. Upon reconnection, the client starts with a clean slate, without any previous session data.
-
Clean Session Flag set to false: When the clean session flag is set to false, the broker creates a persistent session for the client. All session-related information, including subscriptions and queued messages, are preserved by the broker until the next time the client explicitly requests a clean session or disconnects with the clean session flag set to true. If the client reconnects with the clean session flag set to false and the broker still has the session information available, it resumes the existing session, delivering any previously queued messages to the client.
The clean session flag allows clients to control whether they want to maintain a persistent session with the broker or start afresh upon each connection. By setting the flag accordingly, clients can decide whether they want to retain their session state, subscriptions, and queued messages across connections or start with a clean state.
What’s stored in a persistent session?
The MQTT broker identifies the subscribing client within a persistent session primarily based on its client ID. The client ID serves as a unique identifier that allows the broker to associate the session, subscriptions, and other stored information with that particular client. When the client reconnects, it includes its client ID in the CONNECT message, allowing the broker to recognize and resume the persistent session associated with that ID.
In a persistent session in MQTT (Message Queuing Telemetry Transport), the MQTT broker stores certain information related to the client's session and subscriptions. This information helps the broker identify and maintain the state of the subscribing client. Here's what is typically stored in a persistent session:
-
Client ID: Each MQTT client is assigned a unique client ID when it connects to the broker. The client ID serves as an identifier for the client within the MQTT network. In a persistent session, the broker stores the client ID of the subscribing client for future identification and association with its session.
-
Subscriptions: The broker maintains a record of the client's subscriptions. This includes the topics the client has subscribed to and the corresponding Quality of Service (QoS) levels requested for each subscription. This information allows the broker to deliver messages to the client based on its active subscriptions, even if the client disconnects and reconnects.
-
Session State: The persistent session retains the state of the client's session, including any session-specific variables or settings. This may include things like session timers, keep-alive intervals, and other session-related parameters.
-
Queued Messages: If the subscribing client disconnects while it has an active persistent session, the broker may store messages intended for that client during the disconnection period. These queued messages are typically those with QoS levels of 1 or 2, ensuring their reliable delivery to the client upon reconnection.
By storing the client ID, subscriptions, session state, and queued messages, the MQTT broker can provide a seamless experience for clients with persistent sessions. It ensures that the client's subscriptions are maintained, messages are delivered reliably, and the client can resume its session without loss of important information or state.
Key aspects
Here are the key aspects of MQTT persistent sessions:
-
Session State: When a client connects to an MQTT broker, it has the option to establish a persistent session by setting the "clean session" flag to false in the CONNECT message. By doing so, the client indicates to the broker that it wants to maintain its session state across connections.
-
Retaining Subscriptions: With a persistent session, the broker remembers the client's subscriptions and retains them even if the client disconnects. When the client reconnects, the broker automatically restores the subscriptions for the client, ensuring it continues to receive messages from the previously subscribed topics.
-
Queuing of QoS 1 and QoS 2 Messages: If the client has an active persistent session and disconnects, any QoS 1 or QoS 2 messages sent by the broker to the client during the disconnection period are queued by the broker. When the client reconnects and establishes the session, the queued messages are delivered to the client in the order they were originally sent.
Persistent sessions are useful in various scenarios:
-
Uninterrupted Communication: Persistent sessions ensure that clients stay connected to the MQTT broker even if they experience temporary disruptions, such as network outages or intermittent connectivity. It enables uninterrupted communication between clients and the broker.
-
Subscription Maintenance: Clients can rely on persistent sessions to maintain their subscriptions with the broker. This is particularly helpful when subscribing client has limited resources. You want the MQTT broker to store the subscription information of the client and restore the interrupted communication quickly.
By utilizing persistent sessions, MQTT clients can establish a continuous and reliable communication channel with the broker, maintain their subscriptions, and receive missed messages or retained messages upon reconnection.