Skip to content
Related Articles

Related Articles

Improve Article
Fundamental Features of MQTT | Set 4
  • Last Updated : 24 Aug, 2020

Prerequisites –

Keep-Alive Messages :
In MQTT, when clients and brokers are communicating with each other, then connection established between two can be lost due to various reasons like –

  • One of them crashed due to software errors.
  • One of them crashed due to hardware errors.

Due to such software or hardware failures, two components might go out of sync. Such connections are called half-open connections. Hence to check whether connection between clients and broker is still maintained, PINGREQ and PINGRESP can be exchanged between them after specific intervals of time. This message exchange helps to check whether connection between two is still open or not.

The period of time with which these messages are exchanged is called keep-alive period.

So when client publishes message with keep-alive timer to broker, this timer indicates maximum amount of time for which client and broker can remain connected even when no message transmission is being carried out.

Only afte keep-alive timer is expired then new PINGREQ will be sent by the client to broker. PINGRESP will be sent in response by broker and keep-alive timer will be restarted (reset to 0). This message communication will help to ensure that two components are still connected. PINGREQ and PINGRESP messages have no payload. The following two factors have to be taken into consideration for Keep-alive messages –

  • If no PINGRESP is received by client, it should close connection.
  • If no PINGREQ is received from client within 1.5 times of keep-alive timer, broker should close connection.

The default value of keep-alive timer is 60 seconds. The maximum value of this timer can be 18h 12min 15sec. When keepalive timer is set to 0, it indicates that keep-alive mechanism has been deactivated. The following is javascript program indicating publication of messages with keep-alive timer of 5 seconds.


/* jshint esversion : 6 */
"use strict";
// Importing the MQTT package
var mqtt = require('mqtt');
// Creating an instance of the client
var client = mqtt.connect({clientId: "1", keepalive: 5});
// Definiting constants
var topic = "home/kitchen/table";
var message = "Table inside the kitchen";
var options = {retain: false, qos: 1};
// On successful connection
client.on('connect', function () 
    console.log(" After successful connection: "
    // If client is connected, then publish on the 
    topic after every 10seconds
    if (client.connected) 
        setInterval(function () 
            console.log(" Publishing on topic: ", topic);
            client.publish(topic, message, options);
        }, 10000);

In the above program, client with ID: 1 connects with broker with keep-alive timer of 5 seconds. Hence, PINGREQ and PINGRESP will be exchanged between publisher client (ID:1) and broker after every 5 seconds.

On the other hand, publisher’s client will publish messages every 10 seconds to broker. The broker will receive PUBLISH from client id 1 and send an acknowledgment back as Qos level used is 1. The broker will also send PUBLISH to subscriber client 2, which will thus receive published messages after every 10 seconds.

Output –

Figure – Mosquitto Illustration of Keep-Alive Messages

If the publisher client publishes no messages, then after every 5 seconds, only PINGREQ and PINGRESP will be exchanged. Here if subscriber client also specifies keep-alive period, then these PING messages will be exchanged by broker with both clients having IDs 1 and 2 respectively.

Figure – Mosquitto Illustration of Keep-Alive Messages

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.


My Personal Notes arrow_drop_up
Recommended Articles
Page :