1
votes

I am getting the error:

WebSocket connection to 'wss://iot.XXXX.GG:8883/mqtt' failed: Connection closed before receiving a handshake response

When trying to connect to a remote Mosquitto broker over SSL using Javascript Paho library on Windows 10.

What I have already tried is shown in the following listing:

<script type = "text/javascript" language = "javascript">
        var mqtt;
        var reconnectTimeout = 2000;
        var host="iot.XXXX.GG" ;
        var port=8883;

function onConnect() {
// Once a connection has been made, make a subscription and send a message.

        console.log("Connected ");
        message = new Paho.MQTT.Message("Hello World");
        message.destinationName = "sensor1";
        mqtt.send(message);
      }
      function MQTTconnect() {
        console.log("connecting to "+ host +" "+ port);
        mqtt = new Paho.MQTT.Client(host,port,"clientjs");  
        var options = {         
            useSSL:true,
            timeout: 3,
            userName:"abc",
            password:"qweqwe",
            onSuccess: onConnect
         };
        mqtt.connect(options);
};
</script>

Expected results should be a message saying 'Connected. Actual results are shown at the beginning of this post as the error I get.

By the way, my Mosquitto.conf file is:

allow_anonymous false
password_file /etc/mosquitto/passwd

listener 1883 localhost
protocol mqtt

listener 8883
certfile /etc/letsencrypt/live/iot.XXXX.GG/cert.pem
cafile /etc/letsencrypt/live/iot.XXXX.GG/chain.pem
keyfile /etc/letsencrypt/live/iot.XXXX.GG/privkey.pem

# WebSockets - insecure
listener 8083
protocol websockets
#http_dir /home/ΧΧΧΧ/domains/iot.XXXX.GG/public_html
#certfile /etc/letsencrypt/live/iot.XXXX.GG/cert.pem
#cafile /etc/letsencrypt/live/iot.XXXX.GG/chain.pem
#keyfile /etc/letsencrypt/live/iot.XXXX.GG/privkey.pem

1
Edit the question to site your mosquitto.conf file so at can see how you've set it uphardillb
Sorry, I don't understand what you are saying... You want me to list my mosquitto.conf file here?Michael Perakis
The error implies that your broker is not configured to support ssl, I want to see the broker configuration to confirm this. (p.s. "site" should be "show", but phone auto corrected kicked in)hardillb

1 Answers

3
votes

The Paho MQTT client can only connect to a broker configured to run MQTT over WebSockets.

The mosquitto.conf file you have provided has 3 listeners defined.

  1. The default native MQTT listener on port 1883 bound only to localhost
  2. A native MQTT over SSL listener on port 8883 using the letsencrypt certificate
  3. A MQTT over WebSockets listener on port 8083 with the certificates commented out.

If you want to connect from the web page using MQTT over WebSockets and SSL you need to uncomment the certificates from the 3rd listener and change the port you are connecting to in the page to 8083 (not 8883)