MQTT GUIDE
4 min readMQTT Keep-Alive and Ping Explained
MQTT keep-alive is a heartbeat between client and broker. The client promises to send at least one packet within each keep-alive interval; if it has nothing else to send, it sends a PINGREQ and the broker replies with PINGRESP. If the broker receives nothing for 1.5× the interval, it assumes the client is gone and closes the connection.
TCP can lie. A connection can look open to one side while the other has silently vanished — a crashed device, a dropped Wi-Fi link, a firewall that quietly killed an idle session. MQTT keep-alive is the mechanism that lets the broker tell a live client from a dead one.
How keep-alive works
The client declares a keep-alive interval (0–65,535 seconds) in its
CONNECT packet. It then promises to send some packet — a publish, a subscribe, or, if it has nothing
else to say, a PINGREQ — within every interval. The broker answers each PINGREQ with a PINGRESP. Any packet resets the timer.
If the broker hears nothing for 1.5× the interval, it assumes the client is gone and closes the connection. With a 60-second keep-alive, the client must be heard from at least every 90 seconds.
Server keep-alive (MQTT 5.0)
MQTT 5.0 lets the broker push back on the interval a client asks for. If the requested keep-alive is longer than the
broker is willing to allow, it returns its own Server Keep Alive value in the CONNACK,
and the client must adopt that interval instead. This caps how long a connection can sit idle before the broker
is prepared to reclaim it, no matter what the client proposed. MQTT 3.1.1 has no such property, so there the client’s
value always stands.
Why it matters: half-open connections
Without keep-alive, a broker can accumulate half-open connections — sessions it believes are active but whose clients are long gone. These waste resources and leave stale sessions around. Keep-alive forces clients to prove they're alive, so the broker can reclaim dead connections promptly.
Choosing a keep-alive value
Shorter intervals detect failures faster but add ping traffic and wake battery-powered devices more often; longer intervals are lighter but slower to notice a dropped client. Match the interval to how quickly you need to detect a disconnect. Devices that already send data at a steady rate may not need a short keep-alive at all, since their normal traffic keeps the connection active.
Keep-alive in TBMQ
TBMQ monitors keep-alive per connection (checking on an interval set by
MQTT_KEEP_ALIVE_MONITORING_DELAY_MS, every second by default). On timeout it closes the connection with
reason KEEP_ALIVE_TIMEOUT and publishes the client's
Last Will if one was set. For MQTT 5.0 clients it also enforces a
Server Keep Alive: a requested interval above 600 seconds (10 minutes) is capped to that
maximum and returned in the CONNACK. See the
keep-alive docs for the full behavior, including client take-over when the
same client ID reconnects.
Frequently asked questions
What is the MQTT keep-alive interval?
A value in seconds (0 to 65,535) that the client sets in the CONNECT packet. It is the maximum time the client may stay silent; if it has no data to send, it must send a PINGREQ within that window.
What is the 1.5x rule in MQTT keep-alive?
If the broker receives no packet from a client within 1.5 times the keep-alive interval, it considers the client disconnected and closes the connection. For a 60-second keep-alive, that window is 90 seconds.
What are PINGREQ and PINGRESP?
They are the MQTT heartbeat packets. When a client has no application message to send, it sends a PINGREQ and the broker replies with PINGRESP to confirm the connection is still alive.
What happens when keep-alive times out?
The broker closes the connection and publishes the client’s Last Will message if one was configured, so the rest of the system learns the client dropped off. In TBMQ the connection closes with reason KEEP_ALIVE_TIMEOUT.
Can I disable MQTT keep-alive?
Yes — set keep-alive to 0. The client then sends no periodic pings and the broker does not monitor the connection for inactivity. This is generally discouraged because unexpected disconnects go undetected until the next send.
Can the broker override the keep-alive value?
In MQTT 5.0, yes. The broker can return a Server Keep Alive value in CONNACK, and the client must then use that interval instead of the one it requested. TBMQ caps MQTT 5.0 clients at 600 seconds (10 minutes) by default. MQTT 3.1.1 has no such mechanism, so the client’s requested value always stands.
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.