Constant Device Reconnection When Using NodeJs

Hi there,

I am working to develop a NodeJs application that receives data from Losant via Mqtt. I followed the documentation about setting up a device in Javascript, however, when I run my application, the application shows that my device is constantly connecting, causing it to miss when some messages are being passed.

Here is the JS:

Here is the terminal output:

Here is the application overview:

It looks like the device reconnects every 2 seconds, but this is causing it to miss some messages being sent. As you can see from the terminal output, some messages are still being received.

That Disconnect Reason is the key: “Device establishing new connection, closing previous connection”. The Losant device ID serves as the MQTT client ID and, if we receive a valid request to connect using a client ID where we already have a connection using that same client ID, we boot the original connection and allow the new one to establish.

There could be many causes of this, but my money is on one of the following …

  • Elsewhere, there is another piece of hardware using this same client ID to connect, and the two are taking turns bouncing each other off the broker. This one is easy to diagnose so I would start here by creating a new Losant device / access key / secret and using that to connect in the code you provided here. If you’re still seeing the fluttering connections then the cause is probably …
  • Somewhere in your code is an uncaught exception that is causing the application to crash and restart, establishing a new connection before the original has closed. Hooking into the other events we expose in the client and logging the output may help you get to the bottom of this.
1 Like

Creating a new device resolved that issue. I have no idea what other piece of hardware could be using it at the same time, but I will keep an eye on it to see if the issue resurfaces.