I've managed to connect to my Mosquitto broker using a 1.1.0 version of Paho MQTT JS client. However, the client disconnects inmediately after completing the connection. Before using this version, I was using 1.0.1 and it worked. This is my code for the main functions involved:
function onConnectionLost(){
console.log("connection lost");
document.getElementById("status").innerHTML = "Connection Lost";
connected_flag=0;
}
function onFailure(message) {
console.log("Failed");
setTimeout(MQTTconnect, reconnectTimeout);
}
function onConnect() {
connected_flag=1;
document.getElementById("status").innerHTML = "Connected";
console.log("on Connect "+connected_flag);
sub_topics();
}
function MQTTconnect(id) {
var user = document.getElementById("user").value;
var pass = document.getElementById("pass").value;
//mqtt = new Paho.MQTT.Client(host,port,id);
mqtt = new Paho.Client("wss://192.168.1.42:9873/mqtt",id);
//document.write("connecting to "+ host);
var options = {
timeout: 3,
useSSL: true,
userName: user,
password: pass,
onSuccess: onConnect,
onFailure: onFailure,
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
mqtt.onConnected = onConnected;
mqtt.connect(options);
return false;
}
function sub_topics(){
mqtt.subscribe("es2a2/rad");
mqtt.subscribe("e12r/temp");
mqtt.subscribe("e92/hum");
return false;
}
The log on the Mosquitto broker:
1588334688: New client connected from 192.168.1.43 as 33c1olvtc78311srsavrgq53ce (p2, c1, k60, u'admin').
1588334689: Socket error on client mgqilf3dv8dktthb7u4mudlm5i, disconnecting.
EDIT 1
Mosquitto.conf (using Mosquitto version 1.6.8):
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
per_listener_settings true
listener 9873
protocol websockets
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
allow_anonymous false
password_file /etc/mosquitto/passwdfile
listener 8883
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
mosquitto.conf
so we can see how it's setup for the websocket listener. Also include the mosquitto version information – hardillb