Node-lifetime stateful flows

Good Morning folks,

iam new in Node-Red, and i habe some question about the node instancing and lifetime concept, due to implemente a custom node:)

for instance i implemente a own tcp protocol, but i want keep the connection until i disconnect it .

This means i have a Node which connects (and i have to remember the handle/token ) to this connection, i have a node to send an receive over this node, and at least a node to disconnect.

i could store the connection or token in the global context but, maybe there are a better solution!?

in short, i have node which are dependent from each other.

Thanks in advanced

Hi and welcome to the community.

You can do all of this in a single node if you want to. Or, as many nodes do, you can use a configuration node to hold the connection which can then be consumed in multiple node instances. Check out the TCP and MQTT nodes for example.

Typically, a node needing an external persistent connection need do nothing special to keep the connection but does need to handle the (re)deploy and removal from the flow cases. This is done using the close event.

Looking at the code in some of the existing nodes will help you understand better. There is, of course, also documentation about handling the close event in the docs.

ok the configuration node concept could be interessting.. thank you.

But is it possible to create configuration nodes or change it data at runtime!? why iam asking:

i want to itereate many devices , each device has its ohn config , and so i had too run mutiplve flows with sepereate configs, oder sam flow sequentelly with changed config..

OK, I didn't pick up on that. No, config nodes are for when you want the admin of the system to enter the configuration. If you want that to be dynamic, you will need a different way. In that case, you would generally want to allow the configuration to be changed by a suitable incoming msg. On such a message being received, your node would need to close the existing connection (if it was a change rather than a new one) and create the new connection. It would also need to keep a record of the new connection so that you can also close them in the close function.