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