How can I use TLS with Paho MQTT over Javascript?

Viewed 5691

The code I use currently on my website

var client       = null;
var device_is_on    = null;
var hostname       = "********";
var port           = "8003";
var clientId       = "mqtt_js_" + parseInt(Math.random() * 100000, 10);
var device_topic     = "stat/Device_001/POWER";
var status_topic   = "cmnd/Device_001/power";


function connect(){
    client = new Paho.MQTT.Client(hostname, Number(port), clientId);

    client.onConnectionLost = onConnectionLost;
    client.onMessageArrived = onMessageArrived;

    var options = {
        useSSL: true,
        userName : "***",
        password : "********",
        onSuccess: onConnect,
        onFailure: onFail
    };
    client.connect(options);
}


function onConnect(context) {
    options = {qos:0}
    client.subscribe(device_topic, options);
    client.subscribe(status_topic, options);

    var payloadd = "6";

    message = new Paho.MQTT.Message(payloadd);
    message.destinationName = status_topic;
    message.retained = true;
    client.send(message);
}

function onFail(context) {
}

function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0) {
        window.alert("Connection Lost!\nPlease Refresh.");
    }
}

function onMessageArrived(message) {

    if (message.destinationName == device_topic){ 
        var temperature_heading = document.getElementById("device_display");
        temperature_heading.innerHTML = "Air Conditioner: " + message.payloadString;
        if (message.payloadString == "ON" || message.payloadString == "o"){
            device_is_on = true;
        } else {
            device_is_on = false;
        }
    }
}

function device_toggle(){
    if (device_is_on){
        var payload = "off";
        device_is_on = false;
    } else {
        var payload = "on";
        device_is_on = true;
    }

    message = new Paho.MQTT.Message(payload);
    message.destinationName = status_topic;
    message.retained = true;
    client.send(message);
}

What should I put under the "" var options "" section? currently I am getting the error ERR_CERT_AUTHORITY_INVALID in the console of Google Chrome.

Note 1: This code functions perfectly over http but I am converting to https.

Note 2: I use Mosquitto as my MQTT broker.

Help in much appreciated.

2 Answers

It looks like you are using a self signed certificate. This will not be trusted by your browser so it will not connect, raising the error you have shown.

You have 2 options:

  1. Import the certificate into your browser and mark it as trusted (how you do this will vary depending on what browser you are using). This is only really useful for testing/development because normal users should not be importing random certificates as this opens them up to all kinds of security problems.

  2. Get a real trusted certificate for your website and broker. The simplest/cheapest way to do this will be to use letsencrypt. You can then configure mosquitto to use this certificate.

Related