light_ray
light_ray

Reputation: 644

How to connect to wss using the MQTT?

I am trying to make a wss connection using JavaScript(mqttws31.js) client with generated self signed certificate but unable to create a connection and getting the below error.

  1. "Firefox can’t establish a connection to the server at wss://localhost:8883/mqtt."
  2. "Error: AMQJS0011E Invalid state not connected."

I have included the MQTT broker configuration details and JavaScript script code for reference.

MQTT Broker configuration( mosquitto.conf ).

port 8084
persistence true
persistence_file mosquitto.db 

listener 1883 localhost
protocol mqtt

listener 8883
protocol websockets
allow_anonymous true
require_certificate false
cafile C:/Program Files/mosquitto/certs/certs/ca.crt
certfile C:/Program Files/mosquitto/certs/certs/server.crt
keyfile C:/Program Files/mosquitto/certs/certs/server.key
tls_version tlsv1.2

Javascript Client Code:

Below are inputs passing to the function.

host: localhost , port : 8883 and clientID : 1234.

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

        console.log("Connecting to " + host);
        that.client.onConnectionLost = onConnectionLost;
        that.client.onMessageArrived = onMessageArrived;
        that.client.connect({
            onSuccess : onConnect,
            userName: 'user',
            password:'password',
            useSSL: true,
            cleanSession : false
        });
}
function onConnect() {
    console.log('onConnect:');
    that.client.subscribe("mgtl/#", {
        qos : 2,
        onSuccess : function(){
            console.log('Acknowldgement recieved by sender');
        },
        onFailure : function(){
            console.log('Subscribe request has failed or timed out');
        }
    });

    that.client.subscribe("local/ack", {qos : 0});
    console.log('mqtt connected');
}

Can anyone provide me the solution.

Upvotes: 3

Views: 13440

Answers (1)

hardillb
hardillb

Reputation: 59608

As hashed out in the comments, it sounds like your browser didn't trust the CA you used to sign your brokers certificate.

Browsers do not pop up the same dialog about untrusted certificates as they do for HTTPS connection as they expect the code to make a decision about what to do with a connection failure (but I don't think they actually provide the reason in the error messages)

The best way to track this sort of thing down is usually to make sure you check the network tab in the browsers developer tools.

As to why Chrome is not liking the imported CA cert, it may depend on what OS you are on as Chrome uses the system cert store unlike Firefox that maintains it's own iirc.

Upvotes: 2

Related Questions