# Best way to save variables across node-red restarts?

**URL:** <https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315>\
**Category:** Developing Nodes\
**Created:** [18 April 2024 14:31 UTC](https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315 "2024-04-18T14:31:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hansimeister](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@hansimeister](https://discourse.nodered.org/u/hansimeister)\
**Post date:** [18 April 2024 14:31 UTC](https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315/1 "2024-04-18T14:31:15Z")

</div>

Dear Node-redders (are you called like that?)

Just wanted to ask what the best way is to preserve a variable and it's state defined in the .js file of a node. Got some websocket listeners I want to start listening for them again after node-red restarts. How can I do that?

Thank You  
Hansi

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [18 April 2024 16:38 UTC](https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315/2 "2024-04-18T16:38:01Z")

</div>

Hi @hansimeister

If this is outside the basic context of a Nodes config, just use your own mechanism,  
and you need it to be accessed only by your module - just roll your own mechanisms - at least that's what I do.

**Create a folder if it does not exist.**

```auto
module.exports = function(RED) {
    function Init(config) {
        ....
        const stateDir = path.join(RED.settings.userDir, 'super-node-state')
        if (!fs.existsSync(stateDir)) {
            fs.mkdirSync(stateDir)
        }
    }
}

```

Store JSON files in there and read/write/parse them whenever you need, such as checking if there is a state you can use during start up 😉

Many ways, but this is what I do, as the state objects I need to save/read are very complex and some do not use JSON at all.

**EDIT**

There is also the Node Context its self (but I don't use it)  
[https://nodered.org/docs/creating-nodes/context](https://nodered.org/docs/creating-nodes/context)

```auto
var nodeContext = this.context();

```

you need to make sure the Users Node RED is set to use `localfilesystem` (I think) if you want the values to survive a reboot/restart - again I think, I just use my own mech that I have full control over

[https://nodered.org/docs/api/context](https://nodered.org/docs/api/context)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 April 2024 16:56 UTC](https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315/3 "2024-04-18T16:56:58Z")

</div>

Certainly use your own if you don't need/want users of the node to have control. If you do want them to have control, you can use Node-RED's own context methods. I do that, for example, in UIBUILDER's cache node which lets users have full control over whether they want to persist the cache or even make it available to other nodes.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [19 April 2024 11:57 UTC](https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315/4 "2024-04-19T11:57:09Z")

</div>

In the past, others have suggested using an MQTT broker (i.e. mosquito) that supports "retained messages" -- so when an `mqtt in` node connects to a topic it receives the last message that was sent to that topic.

I have not tried this myself, but it sounds pretty elegant and bullet-proof. Searching this forum for "mqtt retain context" yields many interesting threads, like this one: [Access to mqtt retained messages](https://discourse.nodered.org/t/access-to-mqtt-retained-messages/75914)

Edit: _in my pre-caffeine state, I failed to notice the category is "Developing Nodes" -- so this is probably not relevant to what you are doing_

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [19 April 2024 12:11 UTC](https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315/5 "2024-04-19T12:11:58Z")

</div>

> [@hansimeister](#):
>
> Node-redders

🙃

I like "node-reddites" -- sounds like "Luddites" that have discovered a simple tool for building everything without needing to embrace more complex emerging technologies... ;\*)

---

<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:** [18 June 2024 12:12 UTC](https://discourse.nodered.org/t/best-way-to-save-variables-across-node-red-restarts/87315/6 "2024-06-18T12:12:53Z")

</div>

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