# MQTT retain option

**URL:** <https://discourse.nodered.org/t/mqtt-retain-option/17291>\
**Category:** General\
**Created:** [29 October 2019 18:59 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291 "2019-10-29T18:59:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [29 October 2019 18:59 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/1 "2019-10-29T18:59:17Z")

</div>

So if I understand correctly if I set the retain option to true on and MQTT node each time a client subscribes to a topic the server should send the last retained value.

If this is correct I'm doing something wrong. Because on an Arduino Mega on a GSM module I don't seem to recieve these "initial" messages.  
However, to prove my theory I downloaded MQTT Explorer on my computer and subscribed to a topic with the retain flag to true and I still didn't recieve the message.  
This is a screenshot of the configuration on the node:  
 ![29-10-2019%2019_55_33-Home%20Assistant](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/efd74a622a5e539bc64cd83e20c4d85d1c7d3928.png)

This is the configuration on the MQTT Explorer app  
 ![29-10-2019%2019_56_33-MQTT%20Explorer](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f21b7e8bef2d6f52342f8d66b92542e38abb8d79.png) ![29-10-2019%2019_57_23-MQTT%20Explorer](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/abdb5bfba5b9fa195691bdb6df50475bd0bbaf90.png)

And this is the info recieved just after establishing the connection:

 ![29-10-2019%2019_58_05-MQTT%20Explorer](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f3a34d3e7e543ea25285fddbaa52f6ef04f9f2e5.png)

---

<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:** [29 October 2019 19:07 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/2 "2019-10-29T19:07:31Z")

</div>

You PUBLISH messages with the retain flag set to your broker. You then subscribe to that topic and if there is retained data for it it will be delivered with the retain flag set. If you are already subscribed before you publish then the flag won’t be set as it indicates if the data is “live” or not.

---

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [29 October 2019 20:04 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/3 "2019-10-29T20:04:21Z")

</div>

I don't understand. The steps I followed are:  
1- Set the retain flag to true on the broker's output node (this was done literally weeks ago)  
2- Subscribe to that topic

What am I doing wrong?

---

<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:** [29 October 2019 21:23 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/4 "2019-10-29T21:23:31Z")

</div>

I haven't used mqtt explorer, but it looks as if you are publishing to /down/tsetACS and subscribing to /down/deltaTACS.  
On a side note I thought it was not recommended to start topics with /

---

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [29 October 2019 21:37 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/5 "2019-10-29T21:37:58Z")

</div>

> [@Colin](#):
>
> On a side note I thought it was not recommended to start topics with /

Good to know

> [@Colin](#):
>
> I haven't used mqtt explorer, but it looks as if you are publishing to /down/tsetACS and subscribing to /down/deltaTACS.

Sorry, my mistake. This was the first try where I thought you had to publish something to actually recieve the retained values. The screenshoot I posted was of the second try where I just connected and subscribed to the broker (and didn't recieve anything).

 ![29-10-2019%2022_36_38-MQTT%20Explorer](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2b558b9b45d09b286e912d147837a0d2d98fd3e1.png)

What MQTT program would you recomend to try this?

---

<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:** [29 October 2019 21:42 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/6 "2019-10-29T21:42:29Z")

</div>

If you publish a value to the topic (with retain still set) with explorer open does explorer then see it? If yes then what happens if you close the explorer and re-open it?

What mqtt broker are you using?

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [30 October 2019 03:24 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/7 "2019-10-30T03:24:40Z")

</div>

> [@mbc99](#):
>
> 1- Set the retain flag to true on the broker's output node (this was done literally weeks ago)

Something might help a bit. Node-Red is not the broker. Your MQTT server (aDDNSAdressToTheServer) is your broker. Node-Red is either a publisher or a subscriber. Flags set on your messages are dealt with on the Broker.

A good reference to MQTT is on [MQTT Essentials - All Core Concepts Explained](https://www.hivemq.com/mqtt-essentials/)

---

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [30 October 2019 11:50 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/8 "2019-10-30T11:50:46Z")

</div>

Understood. But that doesn't change anything does it?  
I mean, I think I understand correctly how the retain option works and I still don't get de desired/expected results.

---

<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:** [30 October 2019 11:57 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/9 "2019-10-30T11:57:10Z")

</div>

You haven't answered the questions in my last post.

---

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [30 October 2019 12:21 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/10 "2019-10-30T12:21:01Z")

</div>

> [@Colin](#):
>
> You haven't answered the questions in my last post.

Sorry

> [@Colin](#):
>
> What mqtt broker are you using?

I'm using node-red-contrib-mqtt-broker

> **[node-red-contrib-mqtt-broker](https://flows.nodered.org/node/node-red-contrib-mqtt-broker)**
>
> MQTT Broker server on Node-RED

> [@Colin](#):
>
> If you publish a value to the topic (with retain still set) with explorer open does explorer then see it?

If from node red I trigger that MQTT node to send a value I do recieve the value (and all the other values from all the other **input** nodes) on the explorer:  
 ![30-10-2019%2013_13_41-Window](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9ec1c7c6dad115f9455392203a3d77f5b64e729f.png)

It's curious that I have also recieved information from input nodes. Because all the topics listed below /down/deltaTACS are indeed nodes but they are input nodes to the broker.

> [@Colin](#):
>
> If yes then what happens if you close the explorer and re-open it?

The same, I don't recieve the reatined info

---

<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:** [30 October 2019 14:24 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/11 "2019-10-30T14:24:52Z")

</div>

What version of node.js are you using? Assuming it is 8.x 10.x or 12.x then it looks as if the broker is not working. I don't use that broker so can't check out myself. Perhaps someone who does use it can confirm whether it does work for them.  
Just one thought, try it with a topic that does not start with /.

---

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [30 October 2019 14:49 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/12 "2019-10-30T14:49:43Z")

</div>

Still nothing.

Out of curiosity, what broker do you use?

---

<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:** [30 October 2019 16:00 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/13 "2019-10-30T16:00:52Z")

</div>

He/she is using node-red-contrib-mqtt-broker

---

<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:** [30 October 2019 16:04 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/14 "2019-10-30T16:04:22Z")

</div>

@Colin I think their question was directed at you.

---

<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:** [30 October 2019 16:16 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/15 "2019-10-30T16:16:31Z")

</div>

Oops, yes. I am using mosquitto, which is very easy to install. On a debian based system `sudo apt install mosquitto`.

---

<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:** [30 October 2019 16:35 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/16 "2019-10-30T16:35:00Z")

</div>

> [@mbc99](#):
>
> I'm using node-red-contrib-mqtt-broker

AFAIK the mosca broker won't retain messages unless you've setup a database to store them in

see

> **[How to Install and Run The Aedes MQTT Broker on Node-Red](https://stevesnoderedguide.com/install-mosca-mqtt-broker-node-red)**
>
> How to install and use the Aedes node as a local nodejs MQTT broker Rather than use an external broker like mosquitto.

---

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [30 October 2019 16:52 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/17 "2019-10-30T16:52:36Z")

</div>

On the link you attach they mention that the database is needed to maintain the data in case the server is restarted (which makes sense). However, the problem I have is that the server has not been restarted and is not retaining the data

---

<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:** [30 October 2019 16:54 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/18 "2019-10-30T16:54:06Z")

</div>

I hadn't realised that - let me just do a check on my setup

---

<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:** [30 October 2019 17:03 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/19 "2019-10-30T17:03:52Z")

</div>

I've just done a quick test

created mosca broker and set mqtt explorer to monitor it

sent a retained msg using qos1 on topic mosca

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

msg arrives  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/30f3f62744bcbcdca1fd6abd8539d9c2f21c16b1.png)

if I disconnect Mqtt Explorer and re-connect - no mosca topic visible

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

So i'd say it doesn't retain messages

---

<div class="post-metadata">

**Author:** ![mbc99](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mbc99/32/13764_2.png) [@mbc99](https://discourse.nodered.org/u/mbc99)\
**Post date:** [30 October 2019 17:15 UTC](https://discourse.nodered.org/t/mqtt-retain-option/17291/20 "2019-10-30T17:15:18Z")

</div>

So this is the confirmation (before that I still had doubts as to it would be my fault).  
Seems like I will be migrating to Mosquitto...

[Next page](https://discourse.nodered.org/t/mqtt-retain-option/17291.md?page=2)
