MQTTClient.onDisconnect()

Overview

Used to set or replace the disconnect handler for an MQTTClient object.

Added in version 2.1.13

Format

var mqttClient = new MQTTClient(options);

//...

mqttClient.onDisconnect = disconnectHandler;

Value Description Type Required Notes
disconnectHandler A callback function that is called when the session is disconnected. Function Optional When called, the first parameter is a Disconnect object as described below.

Disconnect object properties

Value Description Type Notes
reason A description for the disconnect reason. String  

Example

Copy
/* Only sets the onDisconnect handler after a successful connection.
 */

mqttClient = new MQTTClient({
    broker: "mqtt://mymqtt.example.com"
});

mqttClient.connect(function() {
    mqttClient.onDisconnect = function(event) {
        Logger.info("lost MQTT connection: " + event.reason);
        // try to reconnect after 5 second timeout
        setTimeout(function() { mqttClient.connect(); }, 5000);
    }
});