0
0
IOT Protocolsdevops~6 mins

MQTT keep-alive and timeout in IOT Protocols - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a walkie-talkie conversation with a friend, but you want to make sure your friend is still there listening even when you are not talking. MQTT uses a similar idea to keep connections alive and detect when a device is no longer connected.
Explanation
Keep-Alive Interval
The keep-alive interval is a time period set by the client when it connects to the MQTT broker. It tells the broker how often the client will send a message or a signal to show it is still connected. If the client does not send anything within this time, the broker assumes the connection might be lost.
Keep-alive interval is the maximum time the client can be silent before the broker checks if it is still connected.
PINGREQ and PINGRESP Messages
To maintain the connection during quiet periods, the client sends a small message called PINGREQ to the broker before the keep-alive time expires. The broker replies with PINGRESP to confirm it is still there. This exchange keeps the connection active without sending real data.
PINGREQ and PINGRESP messages are like a handshake to confirm the connection is alive.
Timeout Detection
If the broker does not receive any message or PINGREQ from the client within 1.5 times the keep-alive interval, it considers the client disconnected. The broker then closes the connection and may notify other clients about this event.
Timeout happens when the broker waits too long without hearing from the client and then closes the connection.
Client Responsibility
The client must send some message or a PINGREQ within the keep-alive interval to avoid timeout. If the client is busy or offline, it risks losing the connection and having to reconnect later.
Clients must actively send messages or PINGREQ to keep the connection alive.
Real World Analogy

Imagine you are on a phone call with a friend, but you both agree to say 'Are you there?' every few minutes if no one is talking. If one person stops responding, the other knows the call is lost and hangs up.

Keep-Alive Interval → The agreed time between 'Are you there?' checks during the call
PINGREQ and PINGRESP Messages → 'Are you there?' question and the reply 'Yes, I am here'
Timeout Detection → Hanging up the call when no reply is received after waiting
Client Responsibility → The person who must remember to ask 'Are you there?' to keep the call active
Diagram
Diagram
┌───────────────┐           ┌───────────────┐
│    Client     │           │    Broker     │
└──────┬────────┘           └──────┬────────┘
       │ Keep-Alive Interval Time    │
       │────────────────────────────>│
       │          PINGREQ            │
       │                            │
       │<───────────────────────────│
       │          PINGRESP           │
       │                            │
       │ (No message within 1.5x interval)
       │                            │
       │<───────────────────────────│
       │       Connection Closed     │
This diagram shows the client sending PINGREQ to the broker and receiving PINGRESP to keep the connection alive, and the broker closing the connection if no message is received in time.
Key Facts
Keep-Alive IntervalThe maximum time a client can be silent before the broker expects a message or ping.
PINGREQA message sent by the client to check if the broker is still connected.
PINGRESPA message sent by the broker to confirm it received the client's ping.
TimeoutThe broker closing the connection after not receiving messages within 1.5 times the keep-alive interval.
Client ResponsibilityThe client must send messages or pings regularly to avoid timeout.
Common Confusions
Believing the keep-alive interval is the time the broker waits before disconnecting.
Believing the keep-alive interval is the time the broker waits before disconnecting. The broker waits 1.5 times the keep-alive interval before timing out, not exactly the keep-alive interval.
Thinking PINGREQ contains data payload.
Thinking PINGREQ contains data payload. PINGREQ messages are empty and only serve to check the connection, not to send data.
Assuming the client can stay silent indefinitely without losing connection.
Assuming the client can stay silent indefinitely without losing connection. Clients must send some message or PINGREQ within the keep-alive interval to keep the connection alive.
Summary
MQTT uses a keep-alive interval to ensure clients regularly check in with the broker.
Clients send PINGREQ messages and receive PINGRESP replies to keep the connection active during quiet times.
If the broker does not hear from a client within 1.5 times the keep-alive interval, it closes the connection.