1

I would like to use the mosquitto MQTT broker using Websockets. I am running mosquitto version 1.6.2 on a Debian 8 server. "normal" MQTT works fine, but I am having a problem trying to connect using websocket.

I am using paho javascript client both https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws311.js and https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js

Same problem using

https://unpkg.com/mqtt@3.0.0/dist/mqtt.min.js

Following Steves internet guide tutorials.

The problem is that the client connects to the broker, but is seems that is constantly running the "onConnect" function. In this function it is possible to subscribe to a topic and send a message, which works, but a lot, like 1000 times a second. This is the problem. But the computer running the client as the server starts running 100% CPU.

In the mosquitto broker logs I see the clients only connects once. I can still publish from other client (mosquitto_pub) on the same topic and you can see the message in between the thousands of messages being send from the Paho client.

Does someone know what I can do?

This is a part of the javascript client code:

  function onFailure(message) {
        console.log("Connection Attempt to Host "+host+" Failed");
        console.log(message);
        setTimeout(MQTTconnect, reconnectTimeout);
    }
    function onMessageArrived(msg){
        out_msg="Message received "+msg.payloadString+"<br>";
        out_msg=out_msg+"Message received Topic "+msg.destinationName;
        console.log(out_msg);

    }

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

    console.log("Connected ");
    mqtt.subscribe("sensor1");
    message = new Paho.MQTT.Message("Hello World");
    message.destinationName = "sensor1";
    mqtt.send(message);
  }

  function onConnectionLost(responseObject) {
      if (responseObject.errorCode !== 0) {
        console.log("onConnectionLost:"+responseObject.errorMessage);
      }
    }

  function MQTTconnect() {
    console.log("connecting to "+ host +" "+ port);
    mqtt = new Paho.MQTT.Client(host,port,"clientjs-hoi");
    //document.write("connecting to "+ host);
    var options = {
        timeout: 30,
        onSuccess: onConnect,
        onFailure: onFailure,
         };
    mqtt.onMessageArrived = onMessageArrived;
    mqtt.onConnectionLost = onConnectionLost;

    mqtt.connect(options); //connect
    }

A sample of what happens in the console:

enter image description here

At the server:

enter image description here

In the log at see only once:

1560802249: New client connected from x.x.x.x as clientId-Tdbml6TORG (p1, c1, k60).

  • Which function? Also edit the question to include your code – hardillb Jun 16 at 22:51
  • Also have you tried actually self hosting the latest version of the Paho client to test? – hardillb Jun 17 at 10:48
  • @hardillb I have tried different online clients. Do you have recommendation for a simple client to self host? – David Kester Jun 17 at 20:27
  • You still need to edit the answer to name the function and please add your onMessageArrived function – hardillb Jun 17 at 20:40
  • I have added the missing code and the function that is constantly being called is "onConnect" – David Kester Jun 18 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.