tencent cloud

IoT Hub

Release Notes and Announcements
Release Notes
Product Introduction
Overview
Features
Strengths
Use Cases
Use Limits
Basic Concepts
Getting Started
Quick Start
Scenario 1: Device Interconnection
Scenario 2: Device Status Reporting and Setting
MQTT.fx Connection Guide
Console Guide
Product Management
Rule Engine
Sub-account Access to IoT Hub
Firmware Upgrade
Resource Management
Certificate Management
Developer Manual
Feature Components
Signature Algorithm
Device Authentication
Device Connection Protocol
Gateway Subdevice
Message Communication
Device Shadow
Device Firmware Upgrade
Remote Device Configuration
Resource Management
Device Log Reporting
NTP Service
Device Connection Manual
Device Connection Overview
Connection Based on SDK for C
Connection Based on SDK for Android
Connection Based on SDK for Java
Connection Based on SDK for Python
API Documentation
History
Introduction
API Category
Making API Requests
Device Shadow APIs
Device APIs
CA Certificate APIs
Product APIs
Data Types
Error Codes
FAQs
General
Device Connection and Reporting
Rule Engine
Console
IoT Hub Policy
Privacy Policy
Data Privacy And Security Agreement
Glossary

MQTT Persistent Session

PDF
Focus Mode
Font Size
Last updated: 2024-12-27 15:44:26
IoT Hub supports the MQTT v3.1.1 protocol and supports the quality of service levels of QoS 0 and QoS 1 (but not QoS 2). Using an MQTT persistent session can save the subscription status of devices and subscribed messages that devices have not received. When a device goes offline and goes online again, it can restore the previous session to receive subscribed messages sent when it was offline.

Creating MQTT Persistent Session on Device

When a device is connected to IoT Hub, the CleanSession flag in the variable header part of the Connect message can be set to 0. IoT Hub will determine the session status of the device according to the ClientId when the device is connected. If there is no session currently, it will create a persistent session. If there is an existing session, it will communicate with the device based on the session process.

IoT Hub Response Description

After the device sends a Connect message, IoT Hub will return a Connack message, in which the connection confirmation flag SessionPresent indicates whether IoT Hub includes the session status corresponding to the ClientId when the device is connected. If SessionPresent is 0, no persistent session is created, and the device needs to establish the session status again. If SessionPresent is 1, a persistent session has been created.
After the device is successfully connected, if it enters an existing persistent session, IoT Hub will send the stored QoS 1 messages and unacknowledged QoS 1 messages to the device.
After the device is successfully connected, if a new persistent session is created, IoT Hub will save the subscription status of the device and store the QoS 1 (excluding QoS 0) messages that the device has subscribed to when it is offline. When it goes online again, IoT Hub will send the stored QoS 1 messages and unacknowledged QoS 1 messages to it.
Note:
IoT Hub sends the stored QoS 1 messages sequentially at 500 ms intervals.
Only QoS 1 messages can be stored in a persistent session. Up to 150 messages can be stored for a maximum of 24 hours for each device.

Closing MQTT Persistent Session

The MQTT persistent session can be closed in the following two ways:
When connecting the device to IoT Hub, set the CleanSession flag in the variable header part of the Connect message to 1.
When the device is disconnected for more than 24 hours, the persistent session will be closed automatically.
Note:
Device disconnections include disconnection caused by the device sending the disconnect message and disconnection caused by the device communication timeout.

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback