Achieving Reliable MQTT Connection

Posted on
  • Is there anyone who can help me achieving reliable MQTT connection? I cannot reconnect to the MQTT server after it disconnects for some reason. I tried with two different MQTT server and results are same. Connecting back to the MQTT server on disconnected event does not work.

    disconnected
    disconnected
    disconnected
    disconnected
    disconnected
    disconnected
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    ERROR: Unable to create socket
    
    var wifi = require("Wifi");
    var mqtt = require("tinyMQTT").create("XXXXX", {
        username: "XXXXX",
        password: "XXXXX",
        port: XXXX
    });
    
    mqtt.on("connected", function(){
        console.log("connected");
        mqtt.subscribe("espruino/test");
        isMQTTAlive = true;
    });
    
    mqtt.on("message", function(msg){
        console.log(msg.topic);
        console.log(msg.message);
    });
    
    mqtt.on("published", function(){
        console.log("message sent");
    });
    
    mqtt.on("disconnected", function(){
        console.log("disconnected");
        mqtt.connect();
    });
    
    
    function start() {
      wifi.setHostname("navas178");
      wifi.connect("XXXX", {password:"XXXXX"},function(){
        mqtt.connect();
      });
    }
    
    E.on('init', start);
    
  • This is the console output of my server when the client is trying to reconnect. Any ideas ?

    1484814088: mosquitto version 1.4.8 (build date 2017-01-18 17:13:27+0530) starting
    1484814088: Config loaded from /etc/mosquitto/mosquitto.conf.
    1484814088: Opening ipv4 listen socket on port XXXX.
    1484814089: New connection from XXXXXXXXXX on port XXXX.
    1484814089: New connection from XXXXXXXXXX on port XXXX.
    1484814090: New client connected from XXXXXXXXXX as 5ccf7f84-8675 (c0, k65535, XXXXXXXXXX').
    1484814092: Socket error on client 5ccf7f84-8675, disconnecting.
    1484814096: New connection from XXXXXXXXXX on port XXXX.
    1484814096: New connection from XXXXXXXXXX on port XXXX.
    1484814096: New connection from XXXXXXXXXX on port XXXX.
    1484814096: New connection from XXXXXXXXXX on port XXXX.
    1484814096: Client 5ccf7f84-8675 disconnected.
    1484814096: New client connected from XXXXXXXXXX as 5ccf7f84-8675 (c0, k65535, XXXXXXXXXX').
    1484814096: Socket error on client 5ccf7f84-8675, disconnecting.
    1484814100: New connection from XXXXXXXXXX on port XXXX.
    1484814100: New connection from XXXXXXXXXX on port XXXX.
    1484814101: New connection from XXXXXXXXXX on port XXXX.
    1484814101: New connection from XXXXXXXXXX on port XXXX.
    
  • OK, I think I figured the problem

    a) When it disconnects from the server it throws "disconnected" event twice.
    b) When it reconnects it does not fires "connected" event, but still receives messages.

    Can it be fixed ?

  • Strange - but yes, the code's here: https://github.com/espruino/EspruinoDocs­/blob/master/modules/MQTT.js

    If you wanted to create a pull request that fixed it it'd be great!

    I'm not sure, but it might also be worth reconnecting after a timeout, rather than straight away? It might help...

  • I think that's the tinyMQTT module rather than the core MQTT module. It's not behaviour I've seen but but I haven't used it in a long while TBH. A fix/pull request welcome though.

  • I haven't figured out any fixes yet. I saw the previous post by @Ollie and requested his code, if he can help.

  • See other thread I responded on. If you're using tinyMQTT from here : https://github.com/olliephillips/tinyMQT­T, that's all I have :)

  • Also, you could try the non-tinyMQTT module and see if that helps? I know I recently fixed the double-disconnect issue there.

  • Hey @Gordon,

    Sorry for the delayed response. I was away for a business trip...

    https://github.com/olliephillips/tinyMQT­T

    this one works pretty well so far since these guys fixed the "double" reconnect and enabled the "keepalive" functionality.

    I will test with the official MQTT and will report it back when I get time.

    thanks
    Navas

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Achieving Reliable MQTT Connection

Posted by Avatar for navas @navas

Actions