CONNECT
Fixed header flags: 0000 – all flags reserved, must be 0
CONNECT is the first packet sent by a client after establishing a TCP connection. It carries the client ID, optional credentials (username/password), Will message, keep-alive interval, and session settings. The broker responds with CONNACK. A second CONNECT from the same connection is a protocol error and causes disconnection.
Details
The CONNECT packet initiates an MQTT session. It is always the first packet from client to broker. The variable header contains:
Protocol Name: 4-byte length prefix + "MQTT" (UTF-8) Protocol Version: 5 for MQTT 5.0, 4 for 3.1.1 Connect Flags byte: CleanStart, Will Flag, Will QoS (2 bits), Will Retain, Password Flag, Username Flag Keep Alive: 2-byte unsigned integer, seconds Properties (MQTT 5.0): Session Expiry Interval, Receive Maximum, Maximum Packet Size, Topic Alias Maximum, Request Response Information, Request Problem Information, User Properties, Authentication Method/Data
Payload (in order): Client Identifier, Will Properties (if Will Flag set), Will Topic, Will Payload, Username, Password.
Client ID: must be unique per broker. MQTT 5.0 allows empty Client ID if Clean Start = 1; broker assigns an ID and includes it in CONNACK.
Clean Start (MQTT 5.0): replaces MQTT 3.1.1 CleanSession. When 1, discard any existing session state. When 0, resume existing session. The Session Expiry Interval property controls how long session state persists after disconnect.
Fixed Header
| Field | Value |
|---|---|
| Packet type (bits 7–4) | 1 (0001₂) |
| First byte (hex) | 0x10 |
| Flags (bits 3–0) | 0000 – all flags reserved, must be 0 |
| QoS bits apply | No |
| Direction | client-to-server |
| Added in | MQTT 3.1 |