Custom Node: reoccurring processing, context storage,

Hi All,

I am considering creating my first custom node. I have the functionality working in a function node already. I have two question I did not find the answer to in the blog posts I found:

  1. In my flow, I have an inject node running every second to update calculation in the function node. How that can be done in a custom node?
  2. The code will keep data in the context storage, but I would like that to be kept even if the flow is re-deployed of Node-Red restarts. How can I use the context storage in a custom node? Can I have a property where the user can enter the filesystem storage how it is defined in settings.js?

Thank you for your help.

Use the JS function setInterval (search MDN for info)

You dont necessarily need to store your data in context - as the .js part runs server side, you could just persist to file. If you REALLY want to utilise context, but need persistence in-spite of what the user has set, you could write to file (on node close or modification) & restore from file --> to context (on load)

Generally nodes should avoid flow or global context as they are intended for users, and if you pollute the names space there may be collisions of variable names etc. Also If you do then it is up to the user to configure and set what storage mechanism to use (or not) - you should not try to mandate it. If you use your own then do be aware that some systems may not have a permanent file system per se (eg cloud instances) so you may (or may) not need to take that into account depending on where your node will run.

Thanks a lot. Of course I was not planning to store anything in the flow or global context, just use the node contexts as I do in the function node as well.
I am trying to build a node to which I can send on/off messages and it counts how many times the devices got turned and what the total on-time is. Hence I need to store data and would be great if it also remembers.

How should I go about and implementing the file save process? Should I have a file name field in the node properties to make sire the file is saved in a folder that exists on the OS runs on?

You might like to have a look at node-red-contrib-q-gate. That node has a config option Restore from saved state. If that is selected and the user has enabled a persistent config store then the node will use that to save and restore context. I have not looked at how it does it but it sounds similar to what you want to achieve I think.

Hi Colin,

I checked the node. If you know, can you confirm my understanding of the code?

this.persist = config.persist;

This line reads the checkbox from the property screen of the node, whether the "saving in persistence" is enabled or not. That is straight forward.

Few lines below:

        var node = this;
        var context = node.context();
        var persist = node.persist;

I understand the node, context variable, but I don't think I have seen the persist documented before. So this is an object containing data that is stored in the persistent storage?

Here the code is checking the persist value and setting the detault state:

        if (!persist || typeof state === 'undefined') {
            state = node.defaultState;
            queue = [];
        }

But after this point, this is the only reference I see the state being saved into the context:

context.set('state',state);

This line does not state where to save the variable. So this would only save in persistent storage if that is configured as the default storage in settings.js. Otherwise it will always get erased.

Do I understand this correctly?

No, sorry, I only know that it appeared to do something similar to what you want. I don't know about the implementation.
@drmibell can help with this question?

Yes, you do.

Your node has no way of knowing whether the context store is persistent or not. All it can do after the flow is re-deployed or Node-Red restarts is try to read the data. If it comes back undefined, you know that the storage was volatile.

My gate and q-gate nodes currently use the default storage module, but a branch of node-red-contrib-queue-gate is under development that will allow the user to choose a different module specifically for that node instance. As far as I know, there is no way to give the node access to the localfilesystem module without having it enabled in settings.js and no way to be certain that the file system is non-volatile in any particular hardware configuration.

Thanks a lot for the clarification. I just added file storage to my node-red instance, but still kept the default storage as memory storage. So that would not help me after all. I will wait to see how it works out for you in node-red-contrib-queue-gate.