MQTT retain option

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

This is the configuration on the MQTT Explorer app
29-10-2019%2019_56_33-MQTT%20Explorer 29-10-2019%2019_57_23-MQTT%20Explorer

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

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.

1 Like

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?

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 /

1 Like

Good to know

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).

What MQTT program would you recomend to try this?

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?

1 Like

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

1 Like

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.

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

1 Like

Sorry

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

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

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.

The same, I don't recieve the reatined info

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 /.

1 Like

Still nothing.

Out of curiosity, what broker do you use?

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

@Colin I think their question was directed at you.

2 Likes

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

1 Like

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

see

1 Like

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

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

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

msg arrives
image

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

image

So i'd say it doesn't retain messages

1 Like

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...