# Node initialization order

**URL:** <https://discourse.nodered.org/t/node-initialization-order/68341>\
**Category:** General\
**Created:** [28 September 2022 12:14 UTC](https://discourse.nodered.org/t/node-initialization-order/68341 "2022-09-28T12:14:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [28 September 2022 12:14 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/1 "2022-09-28T12:14:55Z")

</div>

Hi,  
I need to guarantee that a specific function node initializes (its "On Start" code gets executed) before the construction/initialization of any other node in any other flow.

Will it be safe to assume that defining a special tab (containing a single function node) and placing it as the leftmost tab, will force this function node be the first to initialize?

If yes, can I also assume that placing this tab as rightmost, will initialize this node AFTER all the other nodes finished initializing?

Thanks

@knolleary

---

<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:** [28 September 2022 13:35 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/2 "2022-09-28T13:35:00Z")

</div>

> [@omrid](#):
>
> Will it be safe to assume that defining a special tab (containing a single function node) and placing it as the leftmost tab, will force this function node be the first to initialize?

I believe that this is correct. Just bear in mind that as a mostly single-threaded and complex app with lots of async processing, assessing what order things get run in can be quite a challenge. Certainly in the earlier days of node-red though, we were told that the first tab would get executed first.

> [@omrid](#):
>
> can I also assume that placing this tab as rightmost, will initialize this node AFTER all the other nodes finished initializing?

No, I don't believe so. But we would probably need confirmation from @knolleary.

In general, I believe that execution will happen in the order in which things are added to the flows file. But again, bearing in mind the caveat around async operations.

> [@omrid](#):
>
> On Start

I _think_ that any code in on start sections is executed when node-red initialises, I believe that would be before any of the flows start.

---

<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:** [28 September 2022 13:38 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/3 "2022-09-28T13:38:27Z")

</div>

The basic logic is:

1. flows are started in the order they appear in the editor
2. within a flow, nodes are started in the order they were added to the flow

> [@TotallyInformation](#):
>
> I _think_ that any code in on start sections is executed when node-red initialises, I believe that would be before any of the flows start.

No. In a Function node, the 'on start' code is run when the node is started.

---

<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:** [28 September 2022 13:40 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/4 "2022-09-28T13:40:34Z")

</div>

> [@knolleary](#):
>
> No. In a Function node, the 'on start' code is run when the node is started.

Ah, my bad assumption. Good to know.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [28 September 2022 14:09 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/5 "2022-09-28T14:09:05Z")

</div>

Excellent, thank you!  
This is exactly the assertion I was looking for.

> [@knolleary](#):
>
> The basic logic is:
> 
> 1. flows are started in the order they appear in the editor
> 2. within a flow, nodes are started in the order they were added to the flow

But I'm not sure I understood your second comment, Nick:  
Does node-red finish initializing all the nodes (with their 'On Start' sections) before starting the messaging? (else initialized nodes may be sending messages to yet-uninitialized nodes)

> [@knolleary](#):
>
> > [@TotallyInformation](#):
> >
> > I _think_ that any code in on start sections is executed when node-red initialises, I believe that would be before any of the flows start.
> 
> No. In a Function node, the 'on start' code is run when the node is started.

---

<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:** [28 September 2022 14:24 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/6 "2022-09-28T14:24:58Z")

</div>

> [@omrid](#):
>
> Does node-red finish initializing all the nodes (with their 'On Start' sections) before starting the messaging? (else initialized nodes may be sending messages to yet-uninitialized nodes)

No. There are not a separate 'initialise' and 'start' phases. Nodes just get started. The 'on start' code of a Function is nothing special in the lifecycle of the node - it is code that gets run when the node is started, rather than when the node receives a message.

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [28 September 2022 14:58 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/7 "2022-09-28T14:58:26Z")

</div>

> [@knolleary](#):
>
> No. There are not a separate 'initialise' and 'start' phases. Nodes just get started. The 'on start' code of a Function is nothing special in the lifecycle of the node - it is code that gets run when the node is started, rather than when the node receives a message.

Understood, thanks.  
Hence I will programmatically hold back unsolicited messages (e.g. auto-inject, incoming HTTP etc.) until I know their target nodes are ready (maybe set a flag in the rightmost tab).

Could be a nice feature request for the future - to enable global 'pre initialization' & 'post initialization' segments.

Thanks Nick.

---

<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:** [27 November 2022 14:58 UTC](https://discourse.nodered.org/t/node-initialization-order/68341/8 "2022-11-27T14:58:43Z")

</div>

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