最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

MQTT Javascript - Stack Overflow

programmeradmin0浏览0评论

i've been searching a long time now but i haven't found anything useful yet.
I'm trying to implement a MQTT-Javascript-Client. With the release of Mosquitto V1.0 there was a javascript/websocket-client on .0.js released.
But i have no idea how to implement this the right way.
For example: I use the example-server on as broker. When i'm running the following html on my xampp-Server nothing happens and on the broker-side there's no client connected. I assume that there is something incorrect the way i implemented it. It would be great if someone could help 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:1883/',10);
    t.subscribe("mqttdashboard/testtopic", 0);
</script> 
</head>
<body></body></html>

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

i've been searching a long time now but i haven't found anything useful yet.
I'm trying to implement a MQTT-Javascript-Client. With the release of Mosquitto V1.0 there was a javascript/websocket-client on http://mosquitto/js/mosquitto-1.0.js released.
But i have no idea how to implement this the right way.
For example: I use the example-server on http://broker.mqttdashboard. as broker. When i'm running the following html on my xampp-Server nothing happens and on the broker-side there's no client connected. I assume that there is something incorrect the way i implemented it. It would be great if someone could help 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.:1883/',10);
    t.subscribe("mqttdashboard/testtopic", 0);
</script> 
</head>
<body></body></html>

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

Share Improve this question edited Mar 14, 2017 at 8:26 cagdas 1,6442 gold badges14 silver badges27 bronze badges asked Sep 19, 2012 at 16:05 user1683567user1683567 411 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

The server you are connecting to needs to support websockets. The fact that you are connecting to port 1883 suggests to me that it doesn't! The normal thing here would be connecting to port 80 (web) then being upgraded to a websockets connection which happens to talk mqtt. This typically requires the web server to talk to the mqtt broker and be configured to do so, it's not something that happens automatically.

Try using ws://test.mosquitto/ws as your url, it's the only websocket enabled mqtt server I know of at the moment.

The MQTT Dashboard now supports websockets on port 8000. It uses the HiveMQ MQTT broker which supports native websockets as of version 1.4.

Mosquitto.js seems to be deprecated now, so I would strongly suggest to use Eclipse Paho.js as the Javascript MQTT client.

Your code with mosquitto.js would work now when modifying 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.:8000/',10);
    t.subscribe("mqttdashboard/testtopic", 0);
</script> 
</head>
<body></body></html>

Try out broker.hivemq.:8000 for websockets, it supports ws. it should work

I tried with this and it worked so far

<script src="https://cdnjs.cloudflare./ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript">
</script>
<script type="text/javascript">

client = new Paho.MQTT.Client("broker.hivemq.", 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 give a try on cloudmqtt.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论