# MQTT Connection Status of Clients

**URL:** <https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164>\
**Category:** General\
**Created:** [26 October 2019 08:19 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164 "2019-10-26T08:19:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![abajolle](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@abajolle](https://discourse.nodered.org/u/abajolle)\
**Post date:** [26 October 2019 08:19 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/1 "2019-10-26T08:19:03Z")

</div>

Hi dear Node-RED community,

I do have pool of 16 devices running Node-RED and connected trough MQTT.  
There are 15 "slave", each of them connected to a machine and wired to IO & sensors to monitor them.  
All those data are being sent to the "master" that display all that on a dashboard. (Each slave also have its own dashboard for dive-in analysis or monitoring if needed)

I do have a basic function to check the status of the machine, to know if it is running or stopped.  
I also would like to indicate when there is a connection issue or if the machine is powered off.  
The MQTT broker is on the master device.

I do have data coming every few seconds for each slave, so I was considering to have a function to check the time of the last data received for each machine, every 1 minute, and if nothing is received thus I know the machine is either off or facing a connection issue.

I am open to suggestion for better,cleaner way to check the status of those slaves.  
I have seen the keep alive feature, the LWT ... but I am really unsure how to apply those in my case since I am interested by the status of the clients, not the broker.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2019 08:51 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/2 "2019-10-26T08:51:35Z")

</div>

That is exactly what LWT is for. When a client connects to a broker anyone subscribing to the clients LWT topic will get the Online message, and if the client disconnects the subscribers will get the Offline message.  
Note though that the standard LWT system only applies for "unexpected" offline events, such as a power failure. If node-red is stopped by stopping the service then offline does not get sent. The node-red mqtt nodes add an extra message though that allows a message to be sent even for events such as stopping the service.  
Look in the Messages tab of the MQTT config node to find them.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2019 10:36 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/3 "2019-10-26T10:36:29Z")

</div>

> [@Colin](#):
>
> The node-red mqtt nodes add an extra message though that allows a message to be sent even for events such as stopping the service.

Not heard of that one before (I don't use LWT anywhere) - have you got a link to more info on that behaviour?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 October 2019 10:52 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/4 "2019-10-26T10:52:28Z")

</div>

@cymplecy in the broker config node is a tab where you can define a "birth message". It will be published whenever the connection to the broker is established. Not much more to say about it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2019 11:27 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/5 "2019-10-26T11:27:19Z")

</div>

I think it was the "Message sent before disconnecting (close message)" and "Message sent on an unexpected disconnection (will message)" that @cymplecy was referring to.  
I don't know if there is any specific documentation for the close message. The other messages are standard MQTT.  
I make great use of it to, for example, show "Offline" where a temperature should be if the remote device is not connected.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 October 2019 11:45 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/6 "2019-10-26T11:45:14Z")

</div>

but meh... documentation in the info panel

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e6060883cc97b81aa608845342d5daa31a8612fd.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 October 2019 12:11 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/7 "2019-10-26T12:11:23Z")

</div>

> [@cymplecy](#):
>
> Not heard of that one before (I don't use LWT anywhere) - have you got a link to more info on that behaviour?

As always, Hive have the best write up on MQTT:

> **[What is MQTT Last Will and Testament (LWT)? – MQTT Essentials: Part 9](https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/)**
>
> Learn the basics of MQTT Last Will and Testament (LWT) & how it helps maintain system integrity in the event of unexpected client disconnections.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 October 2019 12:13 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/8 "2019-10-26T12:13:56Z")

</div>

> [@Colin](#):
>
> I make great use of it to, for example, show "Offline" where a temperature should be if the remote device is not connected.

All my MQTT connected devices send heartbeat messages on a channel that has an LWT. The heartbeat says "Online", the LWT says "Offline". The topic is `DEVICES/<deviceId>`

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2019 12:27 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/9 "2019-10-26T12:27:26Z")

</div>

So reading up I've come up with this - (Sorry to OP for hijacking thread)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e5dc2aff0563420833531843330b68b4e318017a.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2019 12:40 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/10 "2019-10-26T12:40:13Z")

</div>

I tend to do this, with the same topic for all three as I don't care why it is offline. It may well be more efficient to use booleans rather than strings that have to be compared to test them but I generally think that having unambigous text is a good idea.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c11175f1f785138a97117499d42be00975ffc5bb.png)  
The importance of having Retain set is of course that when the other client connects it will immediately get updated with the status of this client.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [26 October 2019 12:47 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/11 "2019-10-26T12:47:20Z")

</div>

> [@Colin](#):
>
> with the same topic for all three

I made a typo - thanks for spotting it - all 3 topics the same now 🙂  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1584153ba550d54e48190899765043ea1141cb8f.png)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [26 October 2019 13:36 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/12 "2019-10-26T13:36:33Z")

</div>

> [@abajolle](#):
>
> I am interested by the status of the clients, not the broker.

To have a deeper check that a service or application is running and functioning on the slaves as expected, it is not enough to rely on some mqtt status messages as discussed above. They are of course good to have but it could very well happen that the mqtt connection is good while the logic in your service/application has stopped working

You mention that you have a basic function checking the statuses of your machines, I think this is a must so it is good you have it

In my case I have added logic to all my self-written services/applications running in slaves. The "master" sends "heartbeat" requests from NR via mqtt that is received and processed, finally sending a respons back via mqtt. In this way I know that the communication AND logic is still working as foreseen

Also for services/applications eventually running in the same machine as the "master" I use the same methods using NR and mqtt

---

<div class="post-metadata">

**Author:** ![abajolle](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@abajolle](https://discourse.nodered.org/u/abajolle)\
**Post date:** [27 October 2019 13:37 UTC](https://discourse.nodered.org/t/mqtt-connection-status-of-clients/17164/13 "2019-10-27T13:37:27Z")

</div>

Thanks guys !👍

I guess I had understood the LWT wrongly and kind of the opposite, thus I did not look more into it.  
So thanks @Colin for the explanation.

I think i will do as @TotallyInformation & @krambriw, do a combination of a heartbeat signal sent to the slaves something like every 1 minute, with integration of birth/close/will messages to pick-up anytime something happen.
