# Custom Node: reoccurring processing, context storage,

**URL:** https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875
**Category:** Developing Nodes
**Created:** [27 October 2020 08:06 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875 "2020-10-27T08:06:59Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)
#### Post date: [27 October 2020 08:06 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/1 "2020-10-27T08:06:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [31 October 2020 00:00 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/2 "2020-10-31T00:00:47Z")

</div>

> [@nygma2004](#):
>
> 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?

Use the JS function `setInterval` (search MDN for info)

> [@nygma2004](#):
>
> 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

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)

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [31 October 2020 12:14 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/3 "2020-10-31T12:14:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)
#### Post date: [1 November 2020 16:42 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/4 "2020-11-01T16:42:26Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [1 November 2020 17:22 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/5 "2020-11-01T17:22:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)
#### Post date: [3 November 2020 20:14 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/6 "2020-11-03T20:14:18Z")

</div>

Hi Colin,

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

```auto
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:

```auto
        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:

```auto
        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:

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [3 November 2020 22:12 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/7 "2020-11-03T22:12:39Z")

</div>

> [@nygma2004](#):
>
> can you confirm my understanding of the code?

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?

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [4 November 2020 17:33 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/8 "2020-11-04T17:33:57Z")

</div>

> [@nygma2004](#):
>
> Do I understand this correctly?

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](https://github.com/drmibell/node-red-contrib-queue-gate/tree/select_contextStore) 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.

---

<div class="post-metadata">

### Author: ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)
#### Post date: [4 November 2020 17:49 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/9 "2020-11-04T17:49:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [3 January 2021 17:49 UTC](https://discourse.nodered.org/t/custom-node-reoccurring-processing-context-storage/34875/10 "2021-01-03T17:49:08Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
