5月31日 00:26
What are the control packet types in the MQTT protocol? What are their respective purposes?
The MQTT protocol defines multiple control packet types, each with specific functions and formats. Below are the main MQTT control packets and their purposes.
MQTT Control Packet Types
1. CONNECT - Connection Request
- Direction: Client → Broker
- Purpose: Client requests to establish a connection with the Broker
- Key Parameters:
- Client ID: Unique client identifier
- Clean Session: Whether to clear previous session state
- Keep Alive: Heartbeat interval (seconds)
- Username/Password: Authentication information
- Will Message: Last will message (sent when client disconnects abnormally)
- Response: CONNACK
2. CONNACK - Connection Acknowledgment
- Direction: Broker → Client
- Purpose: Confirms whether the connection was successfully established
- Key Parameters:
- Session Present: Whether previous session state is included
- Return Code: Connection result (0 indicates success)
- Return Code Examples:
- 0: Connection successful
- 1: Protocol version not supported
- 2: Client ID rejected
- 3: Server unavailable
- 4: Bad username or password
- 5: Not authorized
3. PUBLISH - Publish Message
- Direction: Bidirectional (Client ↔ Broker)
- Purpose: Publish message to specified topic
- Key Parameters:
- Topic Name: Topic name
- Packet Identifier: Packet identifier (QoS 1/2)
- QoS: Quality of Service level (0/1/2)
- DUP: Whether message is a duplicate
- Retain: Whether to retain message
- Payload: Message content
- Response:
- QoS 0: No response
- QoS 1: PUBACK
- QoS 2: PUBREC → PUBREL → PUBCOMP
4. PUBACK - Publish Acknowledgment (QoS 1)
- Direction: Receiver → Publisher
- Purpose: Acknowledge receipt of QoS 1 message
- Key Parameters:
- Packet Identifier: Corresponding message ID
5. PUBREC - Publish Received (QoS 2)
- Direction: Receiver → Publisher
- Purpose: First phase acknowledgment of QoS 2 message receipt
- Key Parameters:
- Packet Identifier: Corresponding message ID
- Response: PUBREL
6. PUBREL - Publish Release (QoS 2)
- Direction: Publisher → Receiver
- Purpose: Second phase release of QoS 2 message
- Key Parameters:
- Packet Identifier: Corresponding message ID
- Response: PUBCOMP
7. PUBCOMP - Publish Complete (QoS 2)
- Direction: Receiver → Publisher
- Purpose: Third phase completion of QoS 2 message
- Key Parameters:
- Packet Identifier: Corresponding message ID
8. SUBSCRIBE - Subscribe to Topics
- Direction: Client → Broker
- Purpose: Subscribe to one or more topics
- Key Parameters:
- Packet Identifier: Packet identifier
- Topic Filter: Topic filter (supports wildcards)
- QoS: Subscription QoS level
- Response: SUBACK
9. SUBACK - Subscribe Acknowledgment
- Direction: Broker → Client
- Purpose: Confirm subscription result
- Key Parameters:
- Packet Identifier: Corresponding SUBSCRIBE message ID
- Return Codes: Subscription result for each topic
- Return Code Examples:
- 0-2: Success (QoS level)
- 128: Subscription failed
10. UNSUBSCRIBE - Unsubscribe from Topics
- Direction: Client → Broker
- Purpose: Unsubscribe from one or more topics
- Key Parameters:
- Packet Identifier: Packet identifier
- Topic Filter: Topic filter to unsubscribe
- Response: UNSUBACK
11. UNSUBACK - Unsubscribe Acknowledgment
- Direction: Broker → Client
- Purpose: Confirm unsubscription
- Key Parameters:
- Packet Identifier: Corresponding UNSUBSCRIBE message ID
12. PINGREQ - Ping Request
- Direction: Client → Broker
- Purpose: Detect if connection is active
- Trigger Condition: Half of Keep Alive time
- Response: PINGRESP
13. PINGRESP - Ping Response
- Direction: Broker → Client
- Purpose: Respond to ping request, confirm connection is normal
- Response Time: Usually within 1 second
14. DISCONNECT - Disconnect
- Direction: Client → Broker
- Purpose: Actively disconnect
- Features:
- Normal disconnection, will message not sent
- Broker clears client state (Clean Session = true)
Control Packet Format
Fixed Header
All MQTT control packets contain a fixed header:
shell+-----------------+------------------+ | Control Type | Flags | | (4 bits) | (4 bits) | +-----------------+------------------+ | Remaining Length (Variable) | +-------------------------------------+
- Control Type: Control packet type (1-14)
- Flags: Flag bits, with different meanings depending on packet type
- Remaining Length: Remaining length (variable length encoding)
Variable Header
Some packets contain a variable header with packet-specific information:
- Packet Identifier
- Topic Name
- Properties (MQTT 5.0)
Payload
Some packets contain a payload:
- PUBLISH: Message content
- CONNECT: Client information
- SUBSCRIBE: Subscription list
QoS Levels and Packet Flow
QoS 0 Flow
shellClient ──PUBLISH──> Broker
QoS 1 Flow
shellClient ──PUBLISH──> Broker Client <─PUBACK─── Broker
QoS 2 Flow
shellClient ──PUBLISH──> Broker Client <─PUBREC─── Broker Client ──PUBREL──> Broker Client <─PUBCOMP── Broker
Packet Type Summary
| Packet Type | Direction | QoS | Description |
|---|---|---|---|
| CONNECT | Client → Broker | - | Establish connection |
| CONNACK | Broker → Client | - | Connection acknowledgment |
| PUBLISH | Bidirectional | 0/1/2 | Publish message |
| PUBACK | Bidirectional | 1 | Publish acknowledgment |
| PUBREC | Bidirectional | 2 | Publish received |
| PUBREL | Bidirectional | 2 | Publish release |
| PUBCOMP | Bidirectional | 2 | Publish complete |
| SUBSCRIBE | Client → Broker | - | Subscribe to topics |
| SUBACK | Broker → Client | - | Subscribe acknowledgment |
| UNSUBSCRIBE | Client → Broker | - | Unsubscribe from topics |
| UNSUBACK | Broker → Client | - | Unsubscribe acknowledgment |
| PINGREQ | Client → Broker | - | Ping request |
| PINGRESP | Broker → Client | - | Ping response |
| DISCONNECT | Client → Broker | - | Disconnect |
Understanding MQTT control packet types and flows is crucial for implementing MQTT clients and servers.