mqttlibmosquitto

Who sends pings in MQTT?


The keepalive feature in MQTT helps detecting connection problems. That is, if the broker has not received any data from the client in 1.5 * keepalive seconds, it considers the client disconnected. But who should send periodic pings when keepalive is enabled? Broker or client?

The MQTT standard at http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349238 states that the client must send PINGREQ packets if it has no other data to send.

On another hand, mosquitto_connect() function of libmosquitto (https://mosquitto.org/api/files/mosquitto-h.html#mosquitto_connect) has the following description of keepalive parameter:

keepalive: the number of seconds after which the broker should send a PING message to the client if no other messages have been exchanged in that time.

I wanted to clear up this confusion. I lean towards the variant from MQTT standard, but would like to know for sure. Also maybe this indicates a bug in libmosquitto implementation?

Specifically, I use libmosquitto version 1.6.12.


Solution

  • I think this is a bug in documentation of libmosquitto.

    The MQTT standard does not specify a way for the broker to initiate ping messages, only to respond to them. The implementation in libmosquitto client library appears to use the keepalive parameter for periodically sending packets to the broker, just like the standard specifies.

    So it seems the documentation should read:

    keepalive: the number of seconds after which the client should send a PING message to the broker if no other messages have been exchanged in that time.