MQTT publish with retain flag

  1. My goal is:
    To publish to mqtt broker retained message

  2. My actions are:
    Here is code snipped that I run on esp32 board according to this doc:

load(‘api_mqtt.js’);
MQTT.pub(topic, data, 1, true); //true: retain flag

  1. The result I see is:
    Message is not retained as when the subscriber (Node.js app) is restarted the last message is lost. I played with clean session option on that client as well as with QOC options on both esp32 and client to no avail.
    Also, I tried to subscribe for the same topic in node-red and see clearly in it’s debug console that that retain property is false. I’m using RabitMq mqtt broker.

  2. My expectation & question is:
    Message posted is to be retained on mqtt broker and client app will get it upon it’s restart. This is pretty straightforward code and I’m wandering if I’m missing something, if this is related to api_mqtt.js library or mqtt broker.
    Does anyone has this issue with different mqtt broker?

Are you sure your broker is actually retaining messages ? Brokers do need a database and a storage area, that means they have to be configured to support that. Many free brokers don’t. Mosquitto has to be explicitly configured. I don’t know about RabbitMQ (I can guide you to configure Mosquitto).
Last time I used that feature it was working great, and I still have my broker working as expected. Did you actually check with a network sniffer ? The whole purpose of the publish/subscribe paradigm is to decouple the content generator from the content consumer(s), what you see at the subscriber is not what the publisher sent; it is what the broker sends. To check the retain property you have to sniff your WiFi or your Ethernet between your WiFi and your broker.

Thanks for your help @scaprile! Your hint makes perfect sense. I searched the web and found how to configure RabbitMq retained store, see this post, confirmed in logs that it took effect, but still my original problem of missing retained messages on client restart still exists. Will keep looking into it and post when it’s finally resolved.

Actually, my client app receives those retained messages. It just fails to route them further up and that has nothing to do with my original question.
In summary, solution was to configure mqtt broker to store retained messages.
Thanks again, @scaprile for your hint!