Keep alive
The keep-alive interval is how long a client may stay silent. It lets both sides notice a dead connection, which TCP alone can take minutes or hours to report.
How it works
- In the CONNECT packet the client sets a keep-alive interval in seconds, for example
60. - If the client has nothing else to send within that time, its library sends a small PINGREQ; the broker answers with PINGRESP.
- If the broker hears nothing from a client for about 1.5 × the keep-alive interval, it closes the connection and publishes the client's Last Will.
- If the client gets no PINGRESP in time, it treats the connection as dead and reconnects.
Any packet resets the timer, so a device that publishes every few seconds hardly ever sends a ping.
ConnectMQ values
| Setting | Value |
|---|---|
| Maximum keep alive | 600 seconds (10 minutes) |
Keep alive 0 (disabled) or above 600 | Treated as 600 |
| Detection of a silent client | About 1.5 × keep alive |
| Time to send CONNECT after opening the connection | 3 seconds |
The broker applies the 600-second limit without returning a Server Keep Alive value, so a client that asked for more still believes in its own value. Stay at or below 600.
Recommended values
| Client | Keep alive | Why |
|---|---|---|
| Devices and gateways on Ethernet or Wi-Fi | 60 s | Detects an outage within about 90 seconds, negligible traffic |
| Devices on 4G / LTE / NB-IoT | 120–300 s | Fewer wake-ups and less data; carriers may still close idle connections sooner |
| Back-end services | 30–60 s | Fast failover |
| Battery devices that connect, publish and disconnect | Any | The connection is short-lived anyway |
Behind NAT routers and mobile carriers, idle TCP connections are often dropped after a few minutes. If a device reconnects regularly without any error, lower its keep alive.
Keep alive and session minutes
A connected client uses session minutes whether it is publishing or only pinging (limits). Devices that report rarely can save minutes by connecting, publishing and disconnecting instead of staying connected.
Related
- Last Will and Clean sessions
- Troubleshooting: connect/disconnect loops