I cannot connect to the mqtt javascript server
WebSocket error: network error 12031, server connection was reset
I want to subscribe to mqtt message from UI. I am using the following code. I have a mosquitto broker running on my machine, so I gave the url as my IP and it is listening on port number 1883, I gave some random client id
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../paho.javascript-1.0.1/mqttws31-min.js"></script>
<script src="../paho.javascript-1.0.1/mqttws31.js"></script>
<script src="../js/browserMqtt.js"></script>
<script>
// Create a client instance
client = new Paho.MQTT.Client("10.9.177.110", 1883, "100");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
// called when the client connects
function onConnect() {
console.log("onConnect");
client.subscribe("/World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
</script>
</body>
</html>
+3
source to share
2 answers
What type of broker are you trying to connect with? Apart from the IBM MessageSight device, I am not aware of any other brokers that can share the same port for MKTT and MQTT over Websockets.
Since port 1883 is traditionally used for inline MQTT, did you remember adding a new listener for MQTT over Websockets?
Assuming you are using mosquitto 1.4.x, you need to add something like this to your config file:
listerner 1884
protocol websockets
This will add a Websocket listener on port 1884
0
source to share