# How to Resume All Functionality in Node-RED After a Restart?

**URL:** <https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698>\
**Category:** General\
**Tags:** function-node\
**Created:** [5 December 2024 04:39 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698 "2024-12-05T04:39:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [5 December 2024 04:39 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/1 "2024-12-05T04:39:56Z")

</div>

I am facing an issue with session management in Node-RED. Here’s what happens:

1. When a user logs in, the UI creates a session and stores it in the database with a defined expiration time.
2. If Node-RED is not restarted, the expiration mechanism works correctly, and sessions are removed as expected.
3. However, if I restart the Node-RED server, the session data remains stored permanently in the database, and the expiration logic does not resume.

**What I Tried:**

- I attempted to store session data using Node-RED’s **context storage**.
- Unfortunately, after restarting Node-RED, the nodes do not execute, so the expiration logic (using `setTimeout`) does not trigger.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/5879fb204b639d215e719ae23060a338aeeae430.png)

In my **Function node** , I validate the user and create a session with the following logic for session expiration:

const expirationTime = 10 \* 60 \* 1000; // 10 minutes  
setTimeout(async () =\> {  
try {  
console.log("sessionId to be deleted", sessionId);  
const deletedSess = await deleteSession([sessionId]);  
} catch (error) {  
console.error("Error removing session:", error);  
}  
}, expirationTime);

When i login, execute the node-red flows, but if i restart the node-red show below the output

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6ce4ad3d3b617abdee04bcc90849f4c84a6b8c03.png)

---

<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:** [5 December 2024 11:48 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/2 "2024-12-05T11:48:37Z")

</div>

`setTimeout` is not serialisable, so you're not going to be able to do that.

IMHO - after a Node RED restart, would it not be better, just to purge all sessions out of the DB?

---

<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:** [5 December 2024 13:39 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/3 "2024-12-05T13:39:44Z")

</div>

The usual method here would be to have a startup process that runs through the session db and clears out anything that should have expired.

Depending on your user/business requirements, Marcus's approach may be sufficient though. 😄

---

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [6 December 2024 10:03 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/4 "2024-12-06T10:03:50Z")

</div>

@marcus-j-davies @TotallyInformation  
My problem is that once i restart node-red, it will not resume befor functionality

---

<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:** [6 December 2024 10:07 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/5 "2024-12-06T10:07:08Z")

</div>

What functionality?

I think you may need to share a flow, and demo the problem.  
its become unclear what the issue is.

You can use the `inject` node to trigger something at Node RED start up

---

<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:** [6 December 2024 11:16 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/6 "2024-12-06T11:16:38Z")

</div>

> [@LoganathanSambath](#):
>
> once i restart node-red, it will not resume befor functionality

Yes, so you will need some rework. This is normal. As Marcus says, if you can share a bit more info, we may be able to help steer you a bit more explicitly.

---

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [6 December 2024 14:25 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/7 "2024-12-06T14:25:07Z")

</div>

@TotallyInformation @marcus-j-davies

**When I login, this works. After I restart the node, all function node functions are not communicating**  
Eg: I set expire timeout; if node-red not restarted, it worked, if restart didn't work, even i tried contextstorage data, it also not commutincate after restart

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04f4f19853a4a6fcd65a4b68efa97d1a79f3b7d8.png)

---

<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:** [6 December 2024 15:30 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/8 "2024-12-06T15:30:50Z")

</div>

What do you mean "Not Communicating" ?

Do you mean, its stopped clearing down sessions (for those that were already in situ), if so?  
See my previous response below.

> [@marcus-j-davies](#):
>
> `setTimeout` is not serialisable, so you're not going to be able to do that.
> 
> IMHO - after a Node RED restart, would it not be better, just to purge all sessions out of the DB?

The state of a `setTimeout` cannot be "restored" - its only valid for the current instance of the Javascript VM - this is Javascript, not Node RED.

If you mean it no longer works at all (like, not even creating sessions)  
then that is something else - and we need the export of the flow, before we can help further

---

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [7 December 2024 06:08 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/9 "2024-12-07T06:08:43Z")

</div>

> [@marcus-j-davies](#):
>
> The state of a `setTimeout` cannot be "restored" - its only valid for the current instance of the Javascript VM - this is Javascript, not Node RED.

- @marcus-j-davies Thank you, I got it

- "Although I stored the expire time in context storage, the data did not resume after restarting."
- "I wrote the `getExpireTime` code in a function node. When the `/api/login` endpoint is called, does only the function node code work, or are there other options?"  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/optimized/3X/0/5/0516c6f3849d23645eb6048d132ace5ff01767ce_2_690x76.png)

---

<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:** [7 December 2024 08:35 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/10 "2024-12-07T08:35:37Z")

</div>

Add a debug node to check if the api/login node is sending a message. If it is, then add debug code to the function node (you can use `node.warn()` to output to the debug pane) and work out what is going wrong.

---

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [7 December 2024 08:58 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/11 "2024-12-07T08:58:34Z")

</div>

This is working when I login the first time only; if I restart node-red, it does not work. Is there any other option to always run the code?

---

<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:** [7 December 2024 09:12 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/12 "2024-12-07T09:12:11Z")

</div>

Exactly what does not work? Is the http node sending a message?

---

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [7 December 2024 09:50 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/13 "2024-12-07T09:50:50Z")

</div>

That http node only send the message when i call the post endpoint /api/login; if i restart the node-red, the function node code is not resumed  
If there is any other way to alwas work the code?

---

<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:** [7 December 2024 11:02 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/14 "2024-12-07T11:02:02Z")

</div>

> [@LoganathanSambath](#):
>
> the function node code is not resumed

So you mean that the the login api is not called when you restart node red?

---

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [7 December 2024 13:17 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/15 "2024-12-07T13:17:48Z")

</div>

@Colin yes, is there any other method?

---

<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:** [7 December 2024 13:32 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/16 "2024-12-07T13:32:25Z")

</div>

> [@LoganathanSambath](#):
>
> any other method?

Any other method of doing _exactly_ what?  
If you just want to trigger your function when node red restarts then you can use an inject node set to send on startup.

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [7 December 2024 13:54 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/17 "2024-12-07T13:54:23Z")

</div>

Just to be extra clear here, have you enabled persistent context storage?

If not then context will not be saved across a restart of Node-RED as the default context store only keeps things in memory.

---

<div class="post-metadata">

**Author:** ![LoganathanSambath](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/loganathansambath/32/92841_2.png) [@LoganathanSambath](https://discourse.nodered.org/u/LoganathanSambath)\
**Post date:** [7 December 2024 15:50 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/18 "2024-12-07T15:50:42Z")

</div>

> [@hardillb](#):
>
> Just to be extra clear here, have you enabled persistent context storage?

yes i enabled it, even though i didn't use that function codes

---

<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:** [7 December 2024 16:42 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/19 "2024-12-07T16:42:53Z")

</div>

What exactly do you want to happen on restart?

---

<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:** [8 December 2024 09:00 UTC](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698/20 "2024-12-08T09:00:23Z")

</div>

As far as I understand it

The op is creating a timeout (`setTimeout`) for every login - to remove the session out of the DB, but once a restart of Node RED has occurred - the timeout state is lost.

This has been explained to the op.

> [@marcus-j-davies](#):
>
> The state of a `setTimeout` cannot be "restored" - its only valid for the current instance of the Javascript VM - this is Javascript, not Node RED.

The op was advised to maybe use the `inject` node to fire at start up, to purge sessions out of the DB.

> [@marcus-j-davies](#):
>
> IMHO - after a Node RED restart, would it not be better, just to purge all sessions out of the DB?

> [@marcus-j-davies](#):
>
> You can use the `inject` node to trigger something at Node RED start up

> [@TotallyInformation](#):
>
> The usual method here would be to have a startup process that runs through the session db and clears out anything that should have expired.

[Next page](https://discourse.nodered.org/t/how-to-resume-all-functionality-in-node-red-after-a-restart/93698.md?page=2)
