MQTT Javascript

I searched for a long time, but so far have not found anything useful.
I am trying to implement MQTT-Javascript-Client. With the release of Mosquitto V1.0, a javascript / websocket client at http://mosquitto.org/js/mosquitto-1.0.js has been released .

But I have no idea how to implement this correctly.

For example: I use example-server at http://broker.mqttdashboard.com as a broker. When I run the next html on my xampp server, nothing happens and the client does not connect on the broker side. I assume that something is not how I implemented it. It would be great if someone helped me with this.

<html><head> <script type="text/JavaScript" src="mosquitto-1.0.js"></script> <script type="text/JavaScript"> var t = new Mosquitto(); t.connect('ws://broker.mqttdashboard.com:1883/',10); t.subscribe("mqttdashboard/testtopic", 0); </script> </head> <body></body></html> 

I also know about node.js-thing, but I prefer to use websocket-way. Thanks.

+4
source share
3 answers

The server you are connecting to needs web site support. The fact that you are connecting to port 1883 suggests to me that this is not so! The usual thing here will be to connect to port 80 (web) and then upgrade to the web connection that occurs with the mqtt conversation. This usually requires the web server to talk to the mqtt broker and configure itself for this, this is not what happens automatically.

Try using ws: //test.mosquitto.org/ws as your URL, this is the only mqtt server supporting websocket that I know of at the moment.

+6
source

The MQTT toolbar now supports web ports on port 8000. It uses the HiveMQ MQTT broker , which supports native websites from version 1.4.

Mosquitto.js does not seem to be recommended right now, so I highly recommend using Eclipse Paho.js as a Javascript MQTT client.

Now your code with mosquitto.js will work when you change it like this:

 <html><head> <script type="text/JavaScript" src="mosquitto-1.0.js"></script> <script type="text/JavaScript"> var t = new Mosquitto(); t.connect('ws://broker.mqttdashboard.com:8000/',10); t.subscribe("mqttdashboard/testtopic", 0); </script> </head> <body></body></html> 
0
source

Try the broker .hivemq.com: 8000 for websockets, it supports ws. he should work

I tried with this and it has been working so far

 <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"> </script> <script type="text/javascript"> client = new Paho.MQTT.Client("broker.hivemq.com", 8000, "clientId-" + parseInt(Math.random() * 100, 10)); // set callback handlers client.onConnectionLost = onConnectionLost; client.onMessageArrived = onMessageArrived; var options = { onSuccess:onConnect, onFailure:doFail } // connect the client client.connect(options); // called when the client connects function onConnect() { // Once a connection has been made, make a subscription and send a message. console.log("onConnect"); client.subscribe("my/topic1"); } function doFail(e){ console.log(e); } // 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); document.write(message.payloadString); alert("messgaearrived!") } </script> 

and also try on cloudmqtt.com

0
source

Source: https://habr.com/ru/post/1435096/


All Articles