Node MQTT client cant connect over wss

33 views Asked by At

I am unable to connect to a MQTT broker over a wss connection. The connection attempt fails, but no error message is given so I cant determine if it fails some kind of security check, as the client is using a self signed CA cert.

Here is my code for the client connection:

const url = await getServiceUrl(ov);
var ret = undefined
    var fullUrl = 'https://' + require('os').hostname()
    var curPort = window.location.href.split(':')[2].split('/')[0]
    await axios.get(`${fullUrl}:${curPort}/ca`).then(async (res) => { // I have to fetch the cert which is stored on the server
            var cert = res.data
        var opts = {
        "host": url.substring(0, url.lastIndexOf(":")),
        "hostname": url.substring(url.indexOf("://") + 3, url.lastIndexOf(":")),
        "port": url.substring(url.lastIndexOf(":") + 1),
        "protocol": url.substring(0, url.indexOf("://")),
        "ca": [cert],
        "rejectUnauthorized": true // has been true and false, but nothing ever changes
        }
        try {
        const client = mqtt.connect(opts);
        client.on("error", (err) => {
        console.log("mqtt error")
        errorHandler(`Connection failed ${url}`);
        client.end();
        });
        ret = client;
        } catch (e) {
        console.log(e)
        }
    });
    return ret;
}

Note that the client.on(error) and the catch both DO NOT get triggered. Am I using the cert wrong? Why is this failing?

0

There are 0 answers