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

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.

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

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 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. :smile:

1 Like

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

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

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.

1 Like

@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

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.

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

  • "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

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.

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?

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

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?

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

@Colin yes, is there 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.

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.

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

What exactly do you want to happen on restart?

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.

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