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

shell
Client ──PUBLISH──> Broker

QoS 1 Flow

shell
Client ──PUBLISH──> Broker Client <─PUBACK─── Broker

QoS 2 Flow

shell
Client ──PUBLISH──> Broker Client <─PUBREC─── Broker Client ──PUBREL──> Broker Client <─PUBCOMP── Broker

Packet Type Summary

Packet TypeDirectionQoSDescription
CONNECTClient → Broker-Establish connection
CONNACKBroker → Client-Connection acknowledgment
PUBLISHBidirectional0/1/2Publish message
PUBACKBidirectional1Publish acknowledgment
PUBRECBidirectional2Publish received
PUBRELBidirectional2Publish release
PUBCOMPBidirectional2Publish complete
SUBSCRIBEClient → Broker-Subscribe to topics
SUBACKBroker → Client-Subscribe acknowledgment
UNSUBSCRIBEClient → Broker-Unsubscribe from topics
UNSUBACKBroker → Client-Unsubscribe acknowledgment
PINGREQClient → Broker-Ping request
PINGRESPBroker → Client-Ping response
DISCONNECTClient → Broker-Disconnect

Understanding MQTT control packet types and flows is crucial for implementing MQTT clients and servers.

标签:MQTT