MQTT GUIDE
3 min readMQTT Connection: CONNECT and CONNACK
An MQTT session begins with the client sending a CONNECT packet and the broker replying with CONNACK. CONNECT carries the client ID, the clean-start flag, the keep-alive interval, optional credentials and a Last Will; CONNACK returns a reason code and a session-present flag that tells the client whether the broker resumed an existing session.
Before an MQTT client can publish or subscribe, it must open a session with the broker. That always begins with a two-packet handshake: the client sends a CONNECT and the broker replies with a CONNACK.
The connection handshake
CONNECT must be the very first packet on the connection. The broker validates it — protocol version, credentials, client ID — and answers with CONNACK. Only after a successful CONNACK is the session open.
What CONNECT carries
What CONNACK returns
CONNACK carries two things that matter to the client: a reason code (accepted, or why it was refused) and the session-present flag, which tells the client whether the broker resumed an existing session or started a new one. If session-present is false, a client that expected a persistent session knows it must re-subscribe.
In MQTT 5.0, CONNACK can also carry broker-negotiated properties — an assigned client ID, a server keep-alive, the session expiry the broker will honor, and limits such as receive maximum and topic alias maximum — so the client learns the terms of the connection up front. MQTT 3.1.1 returns only the return code and session-present bit.
Connections in TBMQ
TBMQ accepts CONNECTs for MQTT 3.1, 3.1.1 and 5.0, honors Clean Start and reports the session-present flag, and authenticates each CONNECT before the session opens. It also enforces one live connection per client ID — a second CONNECT with the same ID takes over the session (see client take-over). For the supported protocol details, see the MQTT protocol guide and the sessions guide.
Frequently asked questions
What is the difference between CONNECT and CONNACK?
CONNECT is the first packet a client sends to open a session; it carries the client ID, clean-start flag, keep-alive, optional credentials and Last Will. CONNACK is the broker’s reply: it reports whether the connection was accepted (a reason code) and whether an existing session was resumed (the session-present flag).
What is the session-present flag?
A flag in CONNACK that tells the client whether the broker already had stored session state for it (subscriptions and queued messages) and resumed it, versus starting a fresh session. It lets the client know if it needs to re-subscribe.
What happens if the CONNECT is rejected?
The broker returns a CONNACK with a non-zero reason code (for example, bad credentials or an unsupported protocol version) and then closes the network connection. In MQTT 3.1.1 these are return codes; MQTT 5.0 uses richer reason codes.
Does a client send CONNECT every time it wants to publish?
No. CONNECT is sent once, at the start of the connection. It must be the first packet; sending a second CONNECT on the same connection is a protocol violation. After CONNACK, the client can publish and subscribe freely until it disconnects.
What is clean start?
The CONNECT flag that decides whether the broker starts a brand-new session or resumes a stored one. It works together with the session expiry interval — see the persistent sessions guide.
Run it yourself
TBMQ is a free, open-source MQTT broker built to scale. Spin it up in minutes or try the live demo — no install required.