# \[SOLVED\] Frequent MQTT disconnections; "Device Already Connected"

**URL:** <https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96>\
**Category:** Help\
**Created:** [April 13, 2016, 9:31pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96 "2016-04-13T21:31:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 13, 2016, 9:31pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/1 "2016-04-13T21:31:36Z")

</div>

Every minute, I seem to disconnect from Structure’s broker. The device connection log is reporting that my client is already connected. I’m trying to determine whether or not this is [on Node-Red’s side](https://github.com/node-red/node-red/issues/642#issuecomment-209613599) or Losant’s. Here are some logs:

```nohighlight
13 Apr 14:27:10 - [info] Loading palette nodes
13 Apr 14:27:21 - [warn] ------------------------------------------
13 Apr 14:27:21 - [warn] [rpi-gpio] Info : Ignoring Raspberry Pi specific node
13 Apr 14:27:21 - [warn] ------------------------------------------
13 Apr 14:27:21 - [info] Settings file : /home/boneskull/.node-red/settings.js
13 Apr 14:27:21 - [info] User directory : /home/boneskull/.node-red
13 Apr 14:27:21 - [info] Flows file : /home/boneskull/.node-red/flows_geist.json
13 Apr 14:27:21 - [info] Server now running at http://127.0.0.1:1880/
13 Apr 14:27:21 - [info] Starting flows
1460582841456 Device(s) Firmata
13 Apr 14:27:21 - [info] [inject:left state interval] repeat = 60000
13 Apr 14:27:21 - [info] [inject:right state interval] repeat = 60000
13 Apr 14:27:21 - [info] Started flows
1460582841872 Connected Firmata
13 Apr 14:27:22 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: xxxxx@mqtts://broker.getstructure.io:8883
13 Apr 14:27:26 - [info] [nodebot:dink] io ready
13 Apr 14:28:12 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: xxxxx@mqtts://broker.getstructure.io:8883
13 Apr 14:28:28 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: xxxxx@mqtts://broker.getstructure.io:8883
13 Apr 14:29:18 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: xxxxx@mqtts://broker.getstructure.io:8883
13 Apr 14:29:33 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: xxxxx@mqtts://broker.getstructure.io:8883

```

What could cause this behavior?

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 13, 2016, 9:37pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/2 "2016-04-13T21:37:01Z")

</div>

Using TCP vs TLS doesn’t seem to have an effect

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [April 14, 2016, 3:26am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/3 "2016-04-14T03:26:14Z")

</div>

Are you able to get the MQTT disconnect code from your client? The “already connected” message on our end is typically caused by an unclean client disconnect and then a reconnect before the TCP timeout window is exceeded.

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 14, 2016, 3:50am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/4 "2016-04-14T03:50:12Z")

</div>

Thanks Brandon. What does the disconnect code look like? What is the timeout window?

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 14, 2016, 4:37am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/5 "2016-04-14T04:37:33Z")

</div>

I think I figured it out. The default “reconnection period” in Node-Red is 15000ms; I reduced it to the [default](https://github.com/mqttjs/MQTT.js/blob/master/lib/client.js#L22) of 1000ms and we seem to be in business.

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 14, 2016, 8:13am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/6 "2016-04-14T08:13:24Z")

</div>

Nope. It does fine for awhile, then the client goes into an infinite loop of disconnection and reconnection. I imagine it has something to do with what you’re saying, but I’m unsure how to debug. The client is [mqtt](https://www.npmjs.com/package/mqtt) via Node-Red.

I’m fairly certain success w/ reducing the reconnection period was a false positive, as this seems to be the _opposite_ of what should happen. I’m going to increase the reconnection period to 60000ms and see what happens.

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [April 14, 2016, 2:24pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/7 "2016-04-14T14:24:32Z")

</div>

I’m not sure how to get the disconnect code from Node-Red, but that underlying client has a error callback that will return the reason for the disconnect. As a test, I will run Node-Red locally and see if I can reproduce the issue.

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [April 14, 2016, 2:40pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/8 "2016-04-14T14:40:25Z")

</div>

What is your Losant application ID? I’d like to check the activity on our end.

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 17, 2016, 8:26am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/9 "2016-04-17T08:26:41Z")

</div>

`570de5b4fa885601006fb66d`

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [April 19, 2016, 5:35pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/10 "2016-04-19T17:35:17Z")

</div>

So after some investigation on our side, we were able to reproduce the issue. It turns out that the default Node-Red keepalive time of 60 seconds (which means the mqtt client would send a ping every 60 seconds) was longer than the tcp connection inactivity timeout of our load balancer (50 seconds). By default, the keepalive time for the node mqtt module is 15 seconds, so we had not hit this issue using the straight node mqtt module.

We have tweaked our mqtt load balancer settings to allow tcp inactivity of 90 seconds - so now the default Node-Red settings work without issue (I’ve had a Node-Red mqtt connections alive without disconnects for multiple hours now).

Let us know if this fixes the issue for you as well!

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 20, 2016, 2:31am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/11 "2016-04-20T02:31:35Z")

</div>

@spacetc64 Thanks for looking into this.

I had tried reducing the keepalive time in Node-Red below 60s, which should have fixed the problem, correct (it didn’t seem to make a difference at the time)?

These are my settings, which I just modified to use the new domain name:

![](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/53d2a74d71cc91a99d23971cdd1482dbd680aaae.png)

![](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/08ca9158efe10cb592b766d4a9c72cfa8dd8552e.png)

Anyway, so far so good–but last time I _thought_ it was working, it started crapping out in about 20m. I’ll keep an eye on it.

Chris

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 20, 2016, 3:02am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/12 "2016-04-20T03:02:56Z")

</div>

No luck:

```auto
19 Apr 19:27:13 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:27:17 - [info] [nodebot:dink] io ready
19 Apr 19:43:26 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:44:27 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:45:29 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:46:30 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:47:11 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:48:12 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:48:35 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:49:35 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:50:29 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:51:30 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:52:35 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:53:35 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:54:29 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:55:30 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:56:35 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:57:36 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:58:35 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 19:59:35 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 20:00:29 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 20:01:30 - [info] [mqtt-broker:e3b8d80c.342338] Connected to broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883
19 Apr 20:01:35 - [info] [mqtt-broker:e3b8d80c.342338] Disconnected from broker: 570de60b191e390100fc8747@mqtts://broker.losant.com:8883

```

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 20, 2016, 3:04am UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/13 "2016-04-20T03:04:35Z")

</div>

As for what happened on the third line; I have no idea. Node-Red is set to publish device status every one (1) minute.

I’m going to try my own test with the `mqtt` library itself and at least potentially rule out Node-Red as the culprit.

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [April 20, 2016, 1:26pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/14 "2016-04-20T13:26:50Z")

</div>

Correct, a keepalive below 60 should have fixed the problem previously (before we changed load balancer settings).

You don’t need the “legacy MQTT 3.1 support” box checked, although I’m not sure that that makes any difference.

According to the connection log for your device (which you can see on your device page in Losant), almost all of your recent disconnects are due to “Message throughput limit exceeded”. It looks like you are sending quite a number of messages very fast - for instance, in the most recent connection, the connection lasted about 43 seconds but the client sent 641 messages, which is well above the [2 per second limit](https://docs.losant.com/limits/overview/).

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 20, 2016, 8:12pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/15 "2016-04-20T20:12:32Z")

</div>

Whoa! That’s unintended. Let me check.

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 20, 2016, 8:35pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/16 "2016-04-20T20:35:57Z")

</div>

I can confirm this. I don’t know _why_ it’s happening, though.

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 20, 2016, 10:08pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/17 "2016-04-20T22:08:36Z")

</div>

Seems to be a Node-Red bug; I have something set to run every 60000ms but for whatever reason it freaks out and sends at ton at once.

---

<div class="post-metadata">

**Author:** ![boneskull](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/boneskull/32/92_2.png) [@boneskull](https://forums.losant.com/u/boneskull)\
**Post date:** [April 21, 2016, 4:52pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/18 "2016-04-21T16:52:33Z")

</div>

It was user error. 😃 I accidentally had some code recursively creating functions which would publish messages. Everything works great now.

@losant Thanks for helping!

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [April 21, 2016, 9:06pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/19 "2016-04-21T21:06:14Z")

</div>

Awesome! Glad we could get this all figured out.

---

<div class="post-metadata">

**Author:** ![vndk36](https://avatars.discourse-cdn.com/v4/letter/v/ed655f/32.png) [@vndk36](https://forums.losant.com/u/vndk36)\
**Post date:** [September 5, 2016, 3:10pm UTC](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96/20 "2016-09-05T15:10:05Z")

</div>

Hello,

I got a bit of the same issue with my Node-Red sending payload to Losant using MQTT.

I’m using some LORA sensors. They are connected to Node-Red via http and node-red do the payload transcription for losant and send it.

It worked great using a single MQTT output for one device. But since I’ll like to have a single output for all my devices just changing the topic to know witch one it is. It seams like a Gateway and some peripherals will be perfect. But now my MQTT node does not work properly and have a lot of disconnections every 5 secs.

I tried changing the “keep it alive” for more or less that the stock 60s and it didn’t change anything.

Maybe i’m a bit confused with the gateway thing. The topic must be “losant/perippheralID/state” and the MQTT node ID must be the one from the gateway, right?

If someone could help me it will be great

[Next page](https://forums.losant.com/t/solved-frequent-mqtt-disconnections-device-already-connected/96.md?page=2)
