This article is a continuation of the article IoT basics: Introduction to MQTT Protocol
MQTT keepalive is used to know if the connection between sender and receiver is still open and working.
When a connection is established between sender and receiver, the connection usually stays open. However, the sender may not be sending the messages frequently.
If no message was exchanged between the sender and the receiver for a certain amount of time, the sender sends a ping request to the receiver and the receiver sends a response back. This is called the keep-alive interval.
PINGREQ is a ping request message and PINGRESP is the response to the ping.
When opening the connection, the keep-alive period can be set. For example, with mosquito MQTT, the default keep-alive is 60 seconds. That means, if there is no message exchange in 60 seconds, the PINGREQ is sent by the sender.
Definition of keepalive interval from mosquitto docs
keepalive_interval seconds : Set the number of seconds after which the bridge should send a ping if no other traffic has occurred. Defaults to 60. A minimum value of 5 seconds is allowed.
More about the practical use of keepalive interval in the next articles.