# Mqtt reestablishes always new sessions

**URL:** <https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484>\
**Category:** General\
**Created:** [28 June 2022 22:09 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484 "2022-06-28T22:09:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [28 June 2022 22:09 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/1 "2022-06-28T22:09:19Z")

</div>

I am using mosquitto - I have two NodeRed instance on the same machine, one works fine and one which always reconnects. I am using differen mqtt-client IDs, but I get always the following ping pong - the message came through - so functionality is good, but states switches always.  
the mosquitto log shows the reconnection behaviour:

```auto
1656453851: New client connected from 127.0.0.1:47204 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').

Changing keepAlive to higher values - make no difference.
1656453855: New connection from 127.0.0.1:47206 on port 1883.
1656453855: Client mqttClientNodeRedTest already connected, closing old connection.
1656453855: New client connected from 127.0.0.1:47206 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').
1656453865: New connection from 127.0.0.1:47208 on port 1883.
1656453865: Client mqttClientNodeRedTest already connected, closing old connection.
1656453865: New client connected from 127.0.0.1:47208 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').
1656453867: New connection from 127.0.0.1:47210 on port 1883.
1656453867: Client mqttClientNodeRedTest already connected, closing old connection.
1656453867: New client connected from 127.0.0.1:47210 as mqttClientNodeRedTest (p2, c0, k60, u'mwmqtt').
1656453870: New connection from 127.0.0.1:47212 on port 1883.
1656453870: Client mqttClientNodeRedTest already connected, closing old connection.
1656453870: New client connected from 127.0.0.1:47212 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').

```

Increasing keepAlive does not change anything.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 June 2022 22:58 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/2 "2022-06-28T22:58:48Z")

</div>

I suspect you have 2 identical MQTT configs on **one** of your node-red servers. There should be only one (or they should have different client ID)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [28 June 2022 23:02 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/3 "2022-06-28T23:02:06Z")

</div>

I am using the same userid and password in both instances but I use already 2 different client IDs.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 June 2022 23:03 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/4 "2022-06-28T23:03:46Z")

</div>

Look in the drop down where you select the MQTT config - how many entries do you see? Check both node-red servers.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [28 June 2022 23:11 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/5 "2022-06-28T23:11:35Z")

</div>

I see in each instance only one configuration.

I even changed the userID of the test instance and it still keeps the old user-ID- Very strange.

```auto
1656457641: New client connected from 127.0.0.1:49140 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').
1656457641: New connection from 127.0.0.1:49142 on port 1883.
1656457641: Client mqttClientNodeRedTest already connected, closing old connection.
1656457641: New client connected from 127.0.0.1:49142 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').
1656457650: New connection from 127.0.0.1:49144 on port 1883.
1656457650: Client mqttClientNodeRedTest already connected, closing old connection.
1656457650: New client connected from 127.0.0.1:49144 as mqttClientNodeRedTest (p2, c0, k60, u'mwmqtt').
1656457656: New connection from 127.0.0.1:49146 on port 1883.
1656457656: Client mqttClientNodeRedTest already connected, closing old connection.
1656457656: New client connected from 127.0.0.1:49146 as mqttClientNodeRedTest (p2, c0, k60, u'mwmqttMSI').
1656457656: New connection from 127.0.0.1:49148 on port 1883.
1656457656: Client mqttClientNodeRedTest already connected, closing old connection.
1656457656: New client connected from 127.0.0.1:49148 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').
1656457656: New connection from 127.0.0.1:49150 on port 1883.
1656457656: Client mqttClientNodeRedTest already connected, closing old connection.
1656457656: New client connected from 127.0.0.1:49150 as mqttClientNodeRedTest (p2, c0, k60, u'mwmqtt').
1656457656: New connection from 127.0.0.1:49154 on port 1883.
1656457656: Client mqttClientNodeRedTest already connected, closing old connection.
1656457656: New client connected from 127.0.0.1:49154 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqtt').

```

User mwmqtt is no longer used by - but it still connects with this user-ID. The new user (mwmqttMSI) is used in addition.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9510c3258b2c6b1884d8268dc97bec03afc69e5.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/063733402369e203c1d744cbeab3a08e3c05548e.png)

Using a clean session - makes no difference

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 June 2022 23:21 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/6 "2022-06-28T23:21:25Z")

</div>

Search your flow for `mqttClientNodeRedTest` how many are found?

Try restarting both node red servers, does it still happen?

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [28 June 2022 23:37 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/7 "2022-06-28T23:37:36Z")

</div>

So as I said, config is only found once:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/582619e9ef6700d9bb28dd876d610bedcf1168ad.png)

However restarting both NodeRed instances seems that this solved the problem - for the moment. I have no explanation why.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [28 June 2022 23:44 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/8 "2022-06-28T23:44:39Z")

</div>

So in general it works at the moment. After disabling and reenabling the mqtt-in Node - the node works but the status of the node is not updated correctly.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/199c594d25de832ee318fb545e0d75cd4fc78ffe.png)

It says disconnected, but it is connected.

```auto
1656459117: New connection from 127.0.0.1:50616 on port 1883.
1656459117: Client mqttClientNodeRedTest already connected, closing old connection.
1656459117: New client connected from 127.0.0.1:50616 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqttMSI').
1656459574: New connection from 127.0.0.1:50618 on port 1883.
1656459574: Client mqttClientNodeRedTest already connected, closing old connection.
1656459574: New client connected from 127.0.0.1:50618 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqttMSI').

```

So it could be that disabling and reenabling the node - generates trouble ....

Only a complete restart of the NodeRed instance updates the status of the node correctly.

```auto
1656459117: Client mqttClientNodeRedTest already connected, closing old connection.
1656459117: New client connected from 127.0.0.1:50616 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqttMSI').
1656459574: New connection from 127.0.0.1:50618 on port 1883.
1656459574: Client mqttClientNodeRedTest already connected, closing old connection.
1656459574: New client connected from 127.0.0.1:50618 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqttMSI').
1656460151: Client mqttClientNodeRedTest closed its connection.
1656460175: New connection from 127.0.0.1:50620 on port 1883.
1656460175: New client connected from 127.0.0.1:50620 as mqttClientNodeRedTest (p2, c1, k60, u'mwmqttMSI').

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 June 2022 05:46 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/9 "2022-06-29T05:46:23Z")

</div>

Are you in a position to run node-red v3 beta. 3? The were a number of fixes around MQTT connecting/status and I'm fairly certain this is one of the issues fixed.

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [29 June 2022 08:57 UTC](https://discourse.nodered.org/t/mqtt-reestablishes-always-new-sessions/64484/10 "2022-06-29T08:57:51Z")

</div>

No I can't change the version, as those instances are not standalone versions. But as I found out that restarting NodeRed can temporarily fix this reconnection issue, I will wait until version 3 is available. Many thanks so far.
