# Custom node subscribed to flow restart event

**URL:** <https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350>\
**Category:** General\
**Created:** [9 March 2021 15:32 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350 "2021-03-09T15:32:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtaylor](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jtaylor](https://discourse.nodered.org/u/jtaylor)\
**Post date:** [9 March 2021 15:32 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/1 "2021-03-09T15:32:37Z")

</div>

Is it possible for my custom node to subscribe to the flow restart event? This would be to allow me to run some code when the flows restart.

TIA

---

<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:** [9 March 2021 15:38 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/2 "2021-03-09T15:38:45Z")

</div>

> [@jtaylor](#):
>
> when the flows restart

Which flow? All flows on all tabs? just your node? the flow your custom node is on?

> [@jtaylor](#):
>
> This would be to allow me to run some code

When the deploy affects the custom node it will re-initialise it. Put `console.log("xxx") thoughout your custom node .js file - you will see one or more in the console log entries when you re-deploy.

---

<div class="post-metadata">

**Author:** ![jtaylor](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jtaylor](https://discourse.nodered.org/u/jtaylor)\
**Post date:** [9 March 2021 15:47 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/3 "2021-03-09T15:47:19Z")

</div>

The scenario is this, I make a change in the editor and click the Deploy button. That appears to restart all flows. When a given flow restarts, I need to execute code within that flow.

---

<div class="post-metadata">

**Author:** ![jtaylor](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jtaylor](https://discourse.nodered.org/u/jtaylor)\
**Post date:** [9 March 2021 16:03 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/4 "2021-03-09T16:03:28Z")

</div>

I added a ridiculous number of console.log() statements, and it appears that it runs the `node.on("close", function (done) {` function, but the function stops in the middle (I'm guessing it takes too long and the framework just kills it). I think I can work with "close".

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 March 2021 16:11 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/5 "2021-03-09T16:11:22Z")

</div>

> [@jtaylor](#):
>
> I think I can work with "close".

It depends what you are trying to do. Is it the stopping of the flows you care about, or the starting of them?

You cannot do lots of work in the close event - you certainly can't assume the flow you're wired to is all still running as the runtime is busy stopping everything.

Whenever the flows are restarted, your node will be recreated - so its constructor function will get called. If you want it to do something whenever a deploy happens, that is the place to do it.

---

<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:** [9 March 2021 16:21 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/6 "2021-03-09T16:21:21Z")

</div>

> [@jtaylor](#):
>
> I make a change in the editor and click the Deploy button

Note that the Deploy button has a number of options in its drop down selection. If it is set to Modified Nodes, for example, only nodes that have been modified will be restarted, so your node will not see the event (unless it is one of the ones that has been modified of course).

---

<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:** [23 March 2021 16:21 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/7 "2021-03-23T16:21:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [13 March 2025 18:50 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/8 "2025-03-13T18:50:33Z")

</div>



---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [13 March 2025 19:03 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/9 "2025-03-13T19:03:22Z")

</div>

## What is the current state / solution for this problem?

- What is the recommended way to develop a node, that has dependencies?
- Is there a (new) way since 2021 that allows to **detect** if a:
  - complete deploy
  - partial deploy
  - flow restart
  - node-red service restart

is happening? And affecting the node itself, or the "child-nodes" which are depending on it?

(More about it [here...](https://discourse.nodered.org/t/node-hierarchy-oppinions/95979))

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [14 March 2025 07:43 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/10 "2025-03-14T07:43:12Z")

</div>

> [@PizzaProgram](#):
>
> Is there a (new) way since 2021

Isn't there a [hook](https://nodered.org/docs/api/hooks/) for that? I would assume that would be the right way to trigger handlers for events.

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [15 March 2025 02:39 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/11 "2025-03-15T02:39:04Z")

</div>

> [@gregorius](#):
>
> Isn't there a [hook](https://nodered.org/docs/api/hooks/) for that?

I don't think so. Investigated it for 10 hours. It is too low level and would be called every each time some other type of node changes too.

But I've found this here : [RED.events](https://nodered.org/docs/api/ui/events/)

```js
RED.events.on("deploy", function() {
    console.log("deploy happen- ed/ing?")
})

```

_... will test it tomorrow._

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [15 March 2025 10:25 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/12 "2025-03-15T10:25:32Z")

</div>

> [@PizzaProgram](#):
>
> But I've found this here : [RED.events](https://nodered.org/docs/api/ui/events/)

is that a frontend thing? IIRC there are hooks and actions in the backend and events in the frontend but I might well be incorrect on that...

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [20 March 2025 03:37 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/13 "2025-03-20T03:37:30Z")

</div>

... `"deploy"` event never getting fired. 👎

* * *

## Any idea, how to distinguish if NR is closing a node ?

(and not some error)

PS: tryied to find in NR's code where:

> [info] Stopping modified nodes

based on `"flows": {"stopping-modified-nodes": ...` is printed to the consol log, but no files contain it other than the language `locales\..\runtime.json` files.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 March 2025 08:52 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/14 "2025-03-20T08:52:32Z")

</div>

The `close` event handler on the node will be passed a boolean flag to say if the node is stopping because it has been removed from the flow config or not.

Otherwise, there is no way for the node to know _why_ its being closed (ie a deploy of some flavour, or a full shutdown of the runtime).

> **[JavaScript file : Node-RED](https://nodered.org/docs/creating-nodes/node-js#closing-the-node)**

> If the registered listener accepts two arguments, the first will be a boolean flag that indicates whether the node is being closed because it has been removed entirely, or that it is just being restarted. It will also be set to _true_ if the node has been disabled.

```auto
this.on('close', function(removed, done) {
    if (removed) {
        // This node has been disabled/deleted
    } else {
        // This node is being restarted
    }
    done();
});

```

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [20 March 2025 13:08 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/15 "2025-03-20T13:08:52Z")

</div>

> [@knolleary](#):
>
> Otherwise, there is no way for the node to know _why_ its being closed (ie a deploy of some flavour, or a full shutdown of the runtime).

That's a bit of shocking. I've thought there is at least ONE flag, that would tell, if NR is shutting down. After all, it is doing it, and showing the process on the console.

From my point of view...

## a properly written node has to know if:

- NR get's shut down
- (partial?) **deploy** is happening
- it gets **deleted** (at the moment a partial deploy is happening after that)
- it is **changed** , but existed before

Without these it won't be able to handle

- deleting subscriptions,
- restarting or stopping async processes
- report to "master" node

@knolleary Is there any obstacle that would prevent to declare some global variables in NR, like:

```js
RED.starting (bool)
RED.stopping (bool)
RED.on("startstop", callback)

RED.deploying (bool)
RED.on("deploy", callback(partial))

```

---

<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:** [20 March 2025 13:48 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/16 "2025-03-20T13:48:58Z")

</div>

When a node is told to closest should clean up its own subscriptions. If a node has a “master” configuration node that can wait until all the children it knows about have stopped (or told it they have) before calling its own done method.

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [21 April 2025 16:29 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/17 "2025-04-21T16:29:46Z")

</div>

@dceejay Thanks, yes, I understand the basic concept of master-child notifying technics.  
Already implemented into the latest version of node-red-node-arduino module.

But neither the master, nor the child node can not handle, what it does not know. 😟

@knolleary Can you please answer my prev. question?

_Correct me, if I'm wrong... but:_  
From my point of view, this should be very easy correction with lot's of benefits.  
The `node-red-log` is already showing messages like `"Stopping modified nodes"` ... and similar.

You just have to add 1-1 line of code near those.

```log
21 Apr 18:24:56 - [info] Started flows
...
21 Apr 18:25:53 - [info] Stopping modified nodes
21 Apr 18:25:53 - [info] Stopped modified nodes
21 Apr 18:25:53 - [info] Updated flows
21 Apr 18:25:53 - [info] Starting modified nodes
21 Apr 18:25:53 - [info] Started modified nodes

```

Or simply to " **Deploy**" menu command.  
_(After all, NR must know, which nodes must be restarted if someone is clicking that big red button... 😉 )_

---

<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:** [23 April 2025 09:01 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/18 "2025-04-23T09:01:38Z")

</div>

> [@PizzaProgram](#):
>
> But neither the master, nor the child node can not handle, what it does not know.

I have lost track of this thread, what is it that the nodes do not know?

[Edit] What I mean is that it knows if it is being stopped and it knows if it is being started, so what is the difference (as far as it is concerned) between that and a restart?

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [23 April 2025 10:44 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/19 "2025-04-23T10:44:20Z")

</div>

> [@Colin](#):
>
> so what is the difference (as far as it is concerned) between that and a restart?

The huge diff. is:

- How it would/should handle closing / reinitialising.
- Depending on what is the reason of close / or restart.
  - NR service restart? /
  - Flow restart? /
  - full-deploy? /
  - Partial deploy?

### For example:

If there is an external chip connected to the "node" via USB or Wifi, (like the one I'm currently working on via Firmata,) I have to decide what to do with it:

1. If it is a complete **shutdown** of everything, I have to send a RESET to the chip to stop all input events, otherwise the chip's buffer will overflow from collected data it can not send me any more. And I do not have to save the current pin's state.

2. But if this is **only a restart** , the chip should keep all current state of all the ports/pins, keep working as it is now, and I will need to READ the state of the pins/values after restart, to keep track.

3. Also, if it's only a **partial** deploy, nothing should be done, because only a few ports maybe changed,

4. except if the main chip-node (and not only the child nodes) are modified.

Of course I'm talking about a safe, industry grade software, not a "hobby junk", as most of the nodes are currently written. _(Yes, I should not write this down, but this is the truth. Even my own work is not perfect.)_  
But without data, we can not prepare any node how to handle different scenarios.

### A more simple example:

Let's say, you have a home control system, turning on/off your lamps.  
You need to restart the flows at night, because an other module is frozen.  
Do you want that your whole house turns dark?

---

<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:** [23 April 2025 11:11 UTC](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350/20 "2025-04-23T11:11:45Z")

</div>

> [@PizzaProgram](#):
>
> Let's say, you have a home control system, turning on/off your lamps.  
> You need to restart the flows at night, because an other module is frozen.  
> Do you want that your whole house turns dark?

No, of course not. MQTT persisted topics retain the current state and restore it on a restart. It makes no difference whether it is a restart or a stop and later start.

> [@PizzaProgram](#):
>
> If it is a complete **shutdown** of everything, I have to send a RESET to the chip to stop all input events, otherwise the chip's buffer will overflow from collected data it can not send me

Cannot the software controlling the chip cope with the overflow and reset itself if that occurs? It must have to deal with that situation anyway in the case of comms failure for example.

> [@PizzaProgram](#):
>
> And I do not have to save the current pin's state

Is there a problem saving the current pin's state whatever the reason?

> [@PizzaProgram](#):
>
> But if this is **only a restart** , the chip should keep all current state of all the ports/pins, keep working as it is now, and I will need to READ the state of the pins/values after restart, to keep track.

Is there a problem with re-reading the pins values even in the case of a restart?

[Next page](https://discourse.nodered.org/t/custom-node-subscribed-to-flow-restart-event/42350.md?page=2)
