What is Keep Alive MQTT?
Updated: Oct 27

Keep Alive MQTT is the connection-monitoring mechanism that limits how long a client may go without sending an MQTT Control Packet. If the interval is non-zero and the client has nothing else to send, it sends PINGREQ. The broker answers with PINGRESP. These small exchanges help expose an unresponsive connection; they do not guarantee continuous connectivity or successful application work.
For example, a home energy gateway may remain connected to a cloud broker between readings. A Keep Alive interval lets that quiet connection participate in protocol-level monitoring. If a reading already causes the client to send a qualifying Control Packet, that outbound traffic also matters. The mechanism is about MQTT packet activity, rather than whether a person has recently used a dashboard.
The client’s sending obligation, the broker’s silence limit and the client’s wait for a ping response are different rules. Understanding those three boundaries is more useful than treating Keep Alive as one countdown that both endpoints share. This guide explains the rules, works through the timing and reports a reproducible local client experiment.
Where does Keep Alive MQTT monitoring fit into a connection?
A client includes a Keep Alive value in its CONNECT packet. The field is a two-byte integer measured in seconds. In MQTT 3.1.1, it supplies the protocol’s Keep Alive interval. MQTT 5 adds a Server Keep Alive property in CONNACK, allowing the server to specify the value that the client must use instead.
The MQTT guide explains the wider client-to-broker connection and messaging model. Keep Alive operates within an established MQTT connection. It is separate from finding the broker’s hostname, completing authentication or deciding which messages an application may publish.
Element | Meaning |
|---|---|
CONNECT Keep Alive | The client’s proposed or applicable interval, expressed in seconds. |
MQTT 5 Server Keep Alive | A CONNACK property that overrides the CONNECT value when present. |
PINGREQ | The client’s two-byte request for an MQTT ping response. |
PINGRESP | The broker’s two-byte response to PINGREQ. |
Connection recovery | Application and client-library behaviour after a connection closes; separate from the ping exchange. |
A broker may also apply configuration limits. A value that is syntactically valid is not automatically acceptable to every service. Check the actual broker, protocol version and client implementation rather than copying another product’s defaults.
How do PINGREQ and PINGRESP work?
When Keep Alive is non-zero, the client must keep the gap between completing transmission of one MQTT Control Packet and starting the next within the interval. In the absence of other outgoing Control Packets, it must send PINGREQ. This is a client-to-server activity rule, not a requirement for the broker to send an unsolicited heartbeat every interval.
The client establishes the MQTT connection and learns the effective Keep Alive interval.
When ordinary outbound MQTT traffic does not fulfil the sending obligation, the client sends PINGREQ.
The broker receives PINGREQ and sends PINGRESP in response.
The client processes that response through its network loop and records the relevant connection state.
If the required evidence does not arrive, each endpoint applies the rule that governs its side of the connection.
PINGREQ contains the bytes C0 00, and PINGRESP contains D0 00. Each packet is two bytes long, with no variable header or payload. The MQTT 5.0 standard defines these packets and the Keep Alive rules. Their lack of an application payload is an important limit on what a successful response proves.
A returned PINGRESP is evidence that the relevant MQTT responder and communication path responded. It does not establish that a temperature reading was stored, a dashboard was updated or a command was executed. Those outcomes need their own acknowledgements, state records or application checks.
Does normal MQTT traffic reset the need for an idle ping?
Client-originated MQTT Control Packets count towards the client’s activity obligation. A PUBLISH, SUBSCRIBE or another qualifying Control Packet can therefore keep the client from needing a PINGREQ solely to fill an outbound silence gap. It is not necessary to invent a dummy telemetry message just to create protocol activity.
Direction matters. Publications flowing only from broker to client do not themselves constitute a Control Packet sent by the client. The client may send acknowledgements for some deliveries, but a receive-only flow must be analysed according to the packets actually exchanged. Counting visible dashboard updates is not a substitute for tracing the client’s outbound MQTT activity.
There is a second nuance: the protocol permits PINGREQ at any time. A particular client library may choose to ping when it has not received anything, even while other traffic has been sent. Paho Python 2.1.0 checks both inbound and outbound inactivity in its implementation. “Ordinary traffic can fulfil the obligation” should not be turned into “a compliant client will never ping while publishing”.
The Topic guide explains how names and filters select publications. Keep Alive does not need a special topic name, a subscription to a heartbeat topic or a JSON message. An application can design its own health messages, but those are a separate application contract.
Why are the client and broker deadlines different?
For a non-zero interval K, the client must keep its outgoing packet gap within K. The broker must close the Network Connection if it receives no MQTT Control Packet from that client within one and a half times K. The additional half interval is a server-side tolerance boundary; it does not authorise the client to wait 1.5K before sending.
Consider an illustrative connection with K = 30 seconds. To make the arithmetic clear, suppose a client finishes a short PUBLISH at time 20 seconds and the broker receives it at that same illustrated time. We are deliberately ignoring transmission time in this diagram. The client must start its next Control Packet by time 50, while a broker receiving nothing further reaches its 45-second silence limit at time 65.
Illustrative event | Time and consequence |
|---|---|
Last outgoing packet completes | 20 seconds at the client. |
Next client packet must start | By 20 + 30 = 50 seconds. |
Last packet arrives at broker | 20 seconds in this simplified example. |
Broker receives nothing further | Its silence limit is 20 + 1.5 × 30 = 65 seconds. |
Another packet reaches broker at 45 | The broker’s later silence limit moves to 45 + 45 = 90 seconds. |
Real endpoints observe different clocks and different events: client transmission completion is not broker receipt. Scheduling, buffering and path delay also affect what is observed. The example explains the rules; it is not a claim that every real disconnection will be logged at exactly the illustrated timestamp.
The missing-PINGRESP rule is different again. The standard says a client that receives no PINGRESP within a reasonable amount of time after sending PINGREQ should close the connection. It does not prescribe 1.5K as the universal client response deadline. Check the library’s documented or implemented policy and ensure its event loop can run.

What changes with MQTT 5 Server Keep Alive?
MQTT 5 lets a server return Server Keep Alive in CONNACK. When that property is present, the client must use it instead of the Keep Alive value sent in CONNECT. If the property is absent, the server uses the client’s CONNECT value. This makes the accepted connection parameters more informative than the client’s initial configuration alone.
For example, a client might send 60 seconds and receive a Server Keep Alive of 30 seconds. The effective interval is then 30 seconds, with the corresponding 45-second server silence limit. This is a protocol example, not a measured MQTT 5 result from our MQTT 3.1.1 experiment below.
Protocol situation | What to check |
|---|---|
MQTT 3.1.1 connection | CONNECT value and broker acceptance or rejection; no MQTT 5 override property. |
MQTT 5 with Server Keep Alive | Use the returned CONNACK value, rather than assuming the CONNECT proposal remained effective. |
MQTT 5 without that property | The CONNECT Keep Alive value applies. |
Product-specific limits | Read current broker and client documentation and verify effective behaviour. |
The Mosquitto configuration manual describes max_keepalive. Its MQTT 5 handling can return a lower Server Keep Alive, while its documented handling for older protocol versions can reject an excessive interval. That is an implementation policy, not a universal description of every MQTT broker. The currently accessed manual lists zero as the default; installed versions and managed services must be checked independently.
What does Keep Alive zero mean?
A zero value turns off the MQTT Keep Alive mechanism. It removes the requirement to send Control Packets on that particular schedule. It does not promise that the broker, client, network path or application will keep the connection open indefinitely. Administrative closure, endpoint shutdown, authentication policy and other failures still exist.
With MQTT 5, also check whether the server returned an overriding value. Looking only at a CONNECT proposal of zero can miss the effective interval. Likewise, a product may refuse a proposed setting rather than accepting it without limits.
The largest value representable by the field is 65,535 seconds: 18 hours, 12 minutes and 15 seconds. The existence of that range does not make its extremes sensible for a particular deployment. The right choice depends on supported policies, how quickly the application needs failure evidence and the device’s communication constraints.
What did our local MQTT Keep Alive test actually show?
We ran Eclipse Paho MQTT Python 2.1.0 on Python 3.12.14 against a purpose-built scripted TCP peer bound to 127.0.0.1. The peer accepted MQTT 3.1.1 CONNECT packets, returned CONNACK and either answered or deliberately withheld PINGRESP. It was not a full broker. Automatic reconnection was disabled so that the observed connection ending could be examined directly.
The test passed 23 checks, including observed packet behaviour, the CONNECT Keep Alive values, peer cleanup and closed listeners. With a two-second interval, the healthy case produced PINGREQ at about 2.004 seconds and a matching PINGRESP, then another exchange at about 4.006 seconds. Paho’s logs confirmed response processing, and the client remained connected before deliberate cleanup.
When the peer withheld PINGRESP, it observed PINGREQ at about 2.003 seconds and end-of-stream at about 4.006 seconds. The client reported disconnected, and no automatic reconnect occurred. This illustrates this library’s local behaviour under this experiment; it does not establish a universal four-second timeout or measure a production broker’s 1.5K enforcement.
With Keep Alive set to zero, no automatic PINGREQ was observed during a 3.2-second window and the client remained connected until cleanup. That bounded observation supports the narrow test result, not an indefinite-connection claim. Each peer thread exited, the clients stopped and the listeners were confirmed closed.
We did not test MQTT 5 negotiation, a broker’s silence enforcement, TLS, authentication, a real broadband fault, Will delivery, session recovery or application throughput. The small two-second value made the local demonstration short; it is not a recommended production interval. The reproducible script and results are retained with the article’s evidence.
Why must the client network loop keep running?
A configured interval does not send packets by itself. The library needs its network-processing machinery to run so that it can send a ping, process an incoming response and act on missed-response state. A blocked callback or neglected external loop can undermine the expected behaviour even when the configuration appears correct.
The Eclipse Paho client documentation recommends its supported network-loop patterns. An externally integrated loop must arrange the relevant read, write and miscellaneous processing. Paho’s loop_misc handles timing work; adding arbitrary long sleeps around it is not a substitute for following the integration contract.
Separate protocol requirements from library implementation. Our installed Paho 2.1.0 source and local results explain the MQTT 3.1.1 experiment. They do not demonstrate automatic adoption of every MQTT 5 property, or guarantee that another release behaves identically. Verify the supported feature and effective timer in the client version you deploy.
How much traffic do quiet MQTT pings create?
The two MQTT packets in one successful ping exchange total four application-protocol bytes. That is the MQTT packet content, not four bytes on the physical link. TCP and IP headers, transport acknowledgements, TLS, possible retransmissions and other traffic can be much larger than those two small Control Packets.
For an original counting model, suppose 10,000 otherwise quiet clients each perform one ping exchange every 60 seconds, evenly spread through time. That gives 10,000 ÷ 60, or about 166.7 exchanges per second. Multiplying by four gives roughly 666.7 MQTT packet bytes per second across both directions, before the excluded overheads.
At 30 seconds, the same deliberately simplified model gives about 333.3 exchanges and 1,333.3 MQTT bytes per second. Halving the interval doubles this modelled quiet-exchange rate. It does not demonstrate twice the fault-detection performance, predict the actual encrypted bitrate or account for clients whose ordinary traffic changes when they ping.
Averages can also hide bursts. Devices that boot together or use aligned schedules may send checks at similar times. For constrained or battery-powered devices, radio wake-ups and supported sleep behaviour matter alongside byte counts. A long intended sleep needs an appropriate connection and recovery design; a timer setting cannot transmit from a powered-down radio.
How should you choose and verify an interval?
Start with the application’s need for timely failure evidence, then check the supported client and broker behaviour. A shorter interval creates more opportunities to notice silence, but also more protocol activity and less tolerance for long pauses. A longer interval can leave an unusable connection appearing established for longer.
Identify the effective interval and any MQTT 5 override at connection establishment.
Check the client’s missing-PINGRESP policy and ensure its network loop remains responsive.
Include expected device sleep, temporary scheduling stalls and path delays in the design.
Check relevant intermediary idle policies without assuming that every packet refreshes every device’s timer.
Test the user-visible result after failure, including supported reconnection, state recovery and application outcomes.
The general Keep Alive guide compares connection reuse and responsiveness mechanisms at other layers. MQTT Keep Alive is neither an HTTP connection-reuse option nor a direct replacement for TCP keep-alive. Each layer has its own meaning, responder and failure policy.
Our TCP guide explains the underlying byte-stream connection. Transport-level responsiveness does not remove the MQTT client’s Control Packet obligation. Conversely, a successful MQTT ping does not bypass a service’s authorisation, idle-session or administrative closure policies.
What happens to reconnects, sessions and Last Will messages?
Discovering an unresponsive connection and restoring useful application work are separate tasks. A client library may offer reconnection, but the application must still account for authentication, subscriptions, retained session state and uncertain operation outcomes. Keep Alive alone does not recreate those decisions.
MQTT 5 Session Expiry controls how long relevant session state may survive after a Network Connection closes. It is not the same field as Keep Alive. A connection can fail while some session state remains, or close with no retained session state, according to the negotiated settings and implementation limits.
If a Will was configured, an abnormal connection ending can lead to Will publication under the protocol’s rules. MQTT 5 Will Delay and session behaviour can affect the timing. Do not tell an operator that every Keep Alive timeout necessarily produces an immediate offline message exactly at 1.5K. A Will also reports the configured message, rather than independently proving the physical device is dead.
The message broker guide separates broker acceptance, routing, delivery and completed application work. Those boundaries still matter after reconnecting. A responsive new connection does not by itself prove that an earlier command happened exactly once.
How do you diagnose repeated MQTT Keep Alive failures?
Record the protocol version, client release, effective Keep Alive and the observed closure reason before changing values. A duplicate ClientID can cause an existing connection to be taken over, independently of inactivity. MQTT 5 distinguishes Keep Alive timeout and Session taken over reason codes, although the usable evidence depends on what was received and logged.
Confirm the endpoint, client identity, protocol version and successful connection acknowledgement.
Compare CONNECT Keep Alive with any MQTT 5 Server Keep Alive returned in CONNACK.
Trace actual client-originated Control Packets and identify whether idle PINGREQ is sent.
Check whether the broker sends PINGRESP and whether the client network loop processes it.
Compare logs with the correct sender, receipt and response-wait rules; check loop stalls and relevant path policies.
Test one justified change, then verify reconnection and the application’s actual result under an authorised, controlled failure test.
A gateway talking only to a local broker can continue that exchange during some broadband outages if its local dependencies remain available. A connection to a cloud broker still needs the remote path. A smaller Keep Alive value can help expose silence; it cannot repair a failed access line, restore power or create missing upstream capacity.
The practical goal is trustworthy evidence and safe recovery. Choose a supported interval, keep the client loop responsive, inspect the negotiated settings and confirm useful outcomes separately. An apparently open socket is only one part of a working messaging system.
Frequently asked questions
What is Keep Alive in MQTT?
It is the mechanism that limits the client’s outgoing MQTT Control Packet gap. With a non-zero interval and nothing else to send, the client sends PINGREQ and the broker returns PINGRESP.
Does MQTT always send a ping every Keep Alive interval?
No. Other client-originated Control Packets can fulfil the activity obligation, while a client implementation may also send PINGREQ for its own responsiveness checks. Inspect the actual traffic and library behaviour.
Is the MQTT Keep Alive timeout always 1.5 times the interval?
The 1.5-times rule governs the broker’s silence limit for a non-zero Keep Alive. The client’s missing-PINGRESP wait is a separate reasonable-time rule, with implementation-specific behaviour.
What does MQTT Keep Alive zero do?
It turns off that Keep Alive mechanism, subject to the effective MQTT 5 value where an override is returned. It does not prevent other connection closures or guarantee indefinite connectivity.
Can an MQTT 5 broker change the client’s Keep Alive?
Yes. A Server Keep Alive property in CONNACK tells the client to use that value instead of its CONNECT value. MQTT 3.1.1 has no equivalent CONNACK property.
Does PINGRESP prove the device’s application is healthy?
No. It provides evidence that the MQTT responder and path answered. Successful storage, command execution and other application work require separate checks.

