MQTT server, publisher and subscriber on 3 different machines (debians)
messages QoS 2 and retained flag.
I need to get and use this messages just once, not more. The problem is that doesn't matter the configuration I use regarding clean start and retained flag handled, at reboot I always get the last message with the retained flag set as true. I cannot simply ignore the first message because the subscriber could be off while the publisher send it, and in this case I have to process the message. I know I could use the timestamp to detect the last message, but I would like to use just the MQTT option, avoiding to add the timestamp to each single message
I see what you mean now. I can't think of any option other than adding a timestamp (or other unique id) to the message, so you can ignore duplicates at the receiver end.
Yes, and save it in persistent context. By the way at the moment (since this saving must be immediatly in order to avoid to process two times the same message) I use this configuration in the settings,js
This sounds like you are hoping the broker will queue messages up until your subscriber comes back online.
My understanding is that it only retains the last message received on any topic, so if your subscriber is down it can still miss messages regardless of the Retain flag from the publisher.
Personally I would use node context in the function rather than flow. Always keep the scope of data as local as possible.
Bear in mind, though, that this method is not infallible. Node red writes the context out to file at a configurable interval, or on shutdown. That means that a normal shutdown of node-red will guarantee that the most recent value is saved in the file, however if node-red crashes or there is a power fail then the latest value may not yet have been written to disc.
As a matter of interest, what operation is being performed at the receiving end that must not be repeated?
Also, obviously, before setting it in file storage you need to get it and compare it with the one in the current message, and discard the current message if they are the same.
No, as in the docs:
" it caches the values in memory and only writes them out to the file-system every 30 seconds."
That is to prevent it spending all its time saving stuff to disc if a context var is updated rapidly. Imagine what would happen if the context were set 1000 times a second if it wrote the file every time.
Looking here Local Filesystem Context Store : Node-RED I understand that flushInterval works only if cache is set to true. Setting it to false, it writes directly on disk.
If it's not like this, how can I set it to write the changed context after 1 seconds (I means as soon as possible) on disk?
I had a similar but slightly different requirement. As I already have a redis instance in my environment I used redis to store the message ts - all works well however the standard context storage to disk would be the default soon
I didn't know about redis, I'll give it a look even if I think that node-red context is ok for my use, Just I need to understand a bit better how to directly write on disk as soon as possible, since these messages are not so often