MQTT Essentials Part 10: Keep Alive and Client Take-Over
Welcome to the tenth part of the MQTT Essentials, a blog series about the core features and concepts in the MQTT protocol. In this post we will cover the Keep Alive feature of MQTT and why it is especially important for mobile networks.
Problem of half-open TCP connections
As we already know MQTT is based on TCP and that includes a certain guarantee that packets over the internet are transferred “reliable, ordered and error-checked”. Nevertheless it can happen that one of the communicating parties gets out of sync with the other, often due to a crash of one side or because of transmission errors. This state is called a half-open connection. The important point is that the still functioning end is not notified about the failure of the other side and is still trying to send messages and wait for acknowledgements.
The problems with half-open connection increase in mobile networks as the following citation from Andy Stanford-Clark, inventor of the MQTT protocol, explains:
Although TCP/IP in theory notifies you when a socket breaks, in practice, particularly on things like mobile and satellite links, which often “fake” TCP over the air and put headers back on at each end, it’s quite possible for a TCP session to “black hole”, i.e. it appears to be open still, but in fact is just dumping anything you write to it onto the floor.
Andy Stanford-Clark on the topic “Why is the keep-alive needed?“ (Source)
MQTT Keep Alive
In order to work around this issue of half-open connection or at least give a possibility to access if the connection is still open, MQTT provides the keep alive functionality.
The keep alive functionality assures that the connection is still open and both broker and client are connected to one another. Therefore the client specifies a time interval in seconds and communicates it to the broker during the establishment of the connection. The interval is the longest possible period of time, which broker and client can endure without sending a message.
The MQTT specification says the following:
It is the responsibility of the Client to ensure that the interval between Control Packets being sent does not exceed the Keep Alive value. In the absence of sending any other Control Packets, the Client MUST send a PINGREQ Packet.
That means as long as messages are exchanged frequently and the keep alive interval is not exceeded, there is no need to send an extra message to ensure that the connection is still open.
But if the client doesn’t send any messages during the period of the keep alive it must send a PINGREQ packet to the broker to confirm its availability and also make sure the broker is still available.
The broker must disconnect a client, which doesn’t send PINGREQ or any other message in one and a half times of the keep alive interval. Likewise should the client close the connection if the response from the broker isn’t received in a reasonable amount of time.
Keep Alive Flow
Let’s have a look at the keep alive messages in detail. There are two messages involved in the keep alive functionality.
The PINGREQ is sent by the client and indicates to the broker that the client is still alive, even if it hasn’t send any other packets (PUBLISH, SUBSCRIBE, etc..). The client can send a PINGREQ at any time to make sure the network connection is still alive. The PINGREQ packet doesn’t have any payload.
When receiving a PINGREQ the broker must reply with a PINGRESP packet to indicate its availability to the client. Similar to the PINGREQ the packet doesn’t contain any payload.
Good to Know
- If the broker doesn’t receive a PINGREQ or any other packet from a particular client, it will close the connection and send out the last will and testament message (if the client had specified one).
- The MQTT client is responsible of setting the right keep alive value. For example, it can adapt the interval to its current signal strength.
- The maximum keep alive is 18h 12min 15 sec.
- If the keep alive interval is set to 0, the keep alive mechanism is deactivated.
A disconnected client will most likely try to connect again. It could be the case that the broker still has an half-open connection for the same client. In this scenario the MQTT will perform a so-called client take-over. The broker will close the previous connection to the same client (determined by the same client identifier) and establishes the connection with the newly connected client. This behavior makes sure that half-open connection won’t stand in the way of a new connection establishment of the same client.
So that’s the end of part ten in our MQTT Essentials series. We hope you enjoyed the whole series. This was the last official post, but we have planned a MQTT Essential Special for next week, which will be about MQTT over Websockets. And we have already a lot of great ideas for topics we will cover in the future, so stay tuned for more helpful content about MQTT and HiveMQ.
Have a great week and we’ll hope to see you on the next MQTT Monday!
You want to read more blog post related to MQTT? Then sign up for our newsletter and get notified on each new post as soon as its available. If you prefer RSS, you can subscribe to our RSS feed here.