Skip to main content

mqttclient.onconnectionlost(callback)

Register a handler for broken connections

Availability

Agent

Parameters

Name Type Description
callback Function Optional function to be called upon the loss of a connection

Returns

Nothing

Description

This method allows you to register a function that will be called whenever an open connection fails for any reason.

The provided function has one (optional) parameter: reasonCode. If included, this will be passed an integer that indicates the reason for the connection loss. Possible values are:

Value Reason
-1 The socket connection failed. This signals either a network issue, or that the broker chose to abort the connection
-2 The inbound message rate was exceeded. See MQTT rate limits
-3 the inbound maximum message size (currently 256KB) was exceeded
-4 The MQTT message was incorrectly framed, eg. the message header was invalid or out of spec

Only one callback function may be registered at any one time. To clear a previously registered callback, either provide a new function, or pass in null.

Example Code

The following code sets up an MQTT client and connects to the specified MQTT broker. It uses mqttclient.onconnectionlost() to warn the user if the connection is unexpectedly broken.

// CONSTANTS
const url = "tcp://test.mosquitto.org";
const port = 1883
const cid = "imp.mqtt.test." + imp.configparams.deviceid;
const un = "generalgrugger";
const pw = "gaztakh1ghc0mmand";

// GLOBALS
local client = null;

// FUNCTIONS
function error(code) {
    switch (code) {
        case -1: return "Socket Error";
        case 1:  return "Unsupported MQTT version";
        case 2:  return "Client ID rejected";
        case 3:  return "Server unavailable";
        case 4:  return "Invalid username/password";
        case 5:  return "Not authorized";
        default: return "Unknown error";
    }
}

function onMessage(message) {
    // Called on receipt of a message
    server.log("Message \'" + message.message + "\' received under topic \'" + message.topic + "\'");
}

function onConnect(resultCode) {
    // Called when the client connects (or fails to connect) to the MQTT broker
    local s = url + ":" + port.tostring();
    if (resultCode == 0) {
        server.log("Connected to " + s);

        // We're connected to try to subscribe to a topic
        client.subscribe("imp.mqtt.test.pings",
                         mqtt.AT_MOST_ONCE,
                         function(qosMode) {
                             // This is the subscription acknowledgement callback
                             if (qosMode != 0x80) {
                                 client.onmessage(onMessage);
                             }
                         },
                         false);
    }
}

function onLost(reasonCode) {
    // Called when the connection to the MQTT broker is unexpectedly lost
    local s = url + ":" + port.tostring();
    server.log("Connected to " + s + " broken. Code: " + reasonCode);
}

// RUNTIME START
// Instance an MQTT client
client = mqtt.createclient();

// Set up the initial handlers
client.onconnect(onConnect);
client.onconnectionlost(onLost);

// Connect with credentials
local options = {};
options.username <- un;
options.password <- pw;
client.connect(url + ":" + port.tostring(), cid, options);