# \[Feedback Wanted\] Flows Sequence

**URL:** <https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295>\
**Category:** Feature Requests\
**Created:** [4 January 2024 16:48 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295 "2024-01-04T16:48:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 16:48 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/1 "2024-01-04T16:48:18Z")

</div>

Hi Team,  
I would like your feedback on the reasoning that follows.

NR is intended to be used more and more in the industrial sector but the notion of priority in the flows routine is missing. Like `flat sequence` in LabVIEW or `OB40/OB100` in TIA Portal.

Let me explain: an industrial process contains several steps, the process starts with `initialization` then it is `ready to start` then `production` then... Which implies that the routine is carried out according to a sequence.  
PS: The structure may vary and be more complex but that is not the point of this subject.

When NR starts, all flows are started at the same time, so it is not possible to create a sequence of flows.

Would it be possible to create a pre-flow and a post-flow?

This would work as follows (if they are used):

1. Starting NR
2. Loading all flows
3. Starting the pre-flow
4. Waiting the end (a sort of done output)
5. Stopping the pre-flow
6. Starting flows
7. Flows started
8. Stopping flows (as current stop/restart)
9. Starting the post-flow
10. Waiting the end
11. Stopping the post-flow
12. NR stopped

I don't know if it will be in high demand but the routine is more part of a real industrial process.

A concrete use case is the FlowFuse device agent, it would be useful to correctly start/stop the instance when a new snapshot is received.

Thanks for your feedbacks.

PS: It's more of a reflection than a FR for now.

---

<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:** [4 January 2024 17:05 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/2 "2024-01-04T17:05:26Z")

</div>

> [@GogoVega](#):
>
> When NR starts, all flows are started at the same time, so it is not possible to create a sequence of flows.

Not quite true of course, you _can_ sequence flows - but _you_ have to do the sequencing. It is easy enough to arrange gates that start/stop flows either by date/time or by a flag set by another flow or even one entered by a person.

> [@GogoVega](#):
>
> Would it be possible to create a pre-flow and a post-flow?

Would 1 single pre/post be useful though? I would think that most industrial processes - certainly ones that I've modelled in the past - will have many stages, often with complex interlocks and interactions.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 17:40 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/3 "2024-01-04T17:40:31Z")

</div>

> [@TotallyInformation](#):
>
> you _can_ sequence flows

A bit like a sub-flow that is called at a specific time? If so, the problem is that the nodes in it have been instantiated. What I'm looking for here is a flow that executes for the time it takes to call done and then stops - the nodes of this flow are only instantiated when the flow is in the right sequence.

> [@TotallyInformation](#):
>
> Would 1 single pre/post be useful though?

NR not being cyclical it would be too complex (I think) to create a real priority - a sequence of three (pre - prod - post) would already be a good structure

---

<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:** [4 January 2024 18:04 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/4 "2024-01-04T18:04:17Z")

</div>

> [@GogoVega](#):
>
> all flows are started at the same time,

On different note, what does that mean "a flow is started" - I was wondering this because what actually gets started:

- inject node with an insert upon start
- http-in node
- cron-plus node
- ...

does a "flow" actually get started? Or do nodes in flows get started?

I don't know exactly but this question made me wonder.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 January 2024 18:13 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/5 "2024-01-04T18:13:48Z")

</div>

Aren't you referring to a state-machine ? There are [nodes available](https://flows.nodered.org/node/node-red-contrib-xstate-machine) for these kinds of state flows.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 18:54 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/6 "2024-01-04T18:54:35Z")

</div>

> [@gregorius](#):
>
> what does that mean "a flow is started"

A node is defined in a function. The runtime will start by loading all these functions then calling them (config-node first then the normal ones). When all functions have been called, the runtime triggers `flows:started` event.

The principle of the sequence is to **freeze** what is happening inside as long as it is not the right step - when the sequence goes from pre to prod the nodes contained in pre-flows are closed and others are called.

> [@bakman2](#):
>
> There are [nodes available](https://flows.nodered.org/node/node-red-contrib-xstate-machine)

Thanks but this must be done at the runtime level, not at the node level.

It looks like this - that's why I'm simplifying it into just three steps:

 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f3f9dfe4b815060665eb603907dad3540cf2fa86.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 January 2024 19:01 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/7 "2024-01-04T19:01:51Z")

</div>

This diagram _is_ a state machine, did you look at the node I linked to ?

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 19:06 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/8 "2024-01-04T19:06:34Z")

</div>

Yes, but the problem is that when the runtime is stopped, this node is closed regardless of what machine state it is in.

---

<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:** [4 January 2024 19:13 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/9 "2024-01-04T19:13:45Z")

</div>

> [@GogoVega](#):
>
> the problem is that the nodes in it have been instantiated. What I'm looking for here is a flow that executes for the time it takes to call done and then stops - the nodes of this flow are only instantiated when the flow is in the right sequence.

But that isn't how Node-RED works is it. Nodes are initialised when Node-RED starts, an instance is instantiated as soon as they are part of a flow and Node-RED starts. These are fundamental to the structure.

When designing Nodes, you need to take that into account.

If you want code that only instantiates when a flow runs, this is not impossible thanks to the flexibility of node.js but nodes would have to be specifically designed to do that. And it brings its own set of issues such as startup time.

> [@bakman2](#):
>
> Aren't you referring to a state-machine ?

That is what I was intimating.

> [@GogoVega](#):
>
> Yes, but the problem is that when the runtime is stopped, this node is closed regardless of what machine state it is in.

That is simply a matter of saving state sensibly though isn't it. If a specific state machine node doesn't do it, you could always create your own state-machine flow.

* * *

I think that you need to think through the problem from a Node-RED architecture perspective. As far as I can see, Node-RED is perfectly capable of constructing what you are asking for but it will not look like a flow created in LabVIEW which works in a very different way.

---

<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:** [4 January 2024 19:15 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/10 "2024-01-04T19:15:07Z")

</div>

> [@GogoVega](#):
>
> A node is defined in a function. The runtime will start by loading all these functions then calling them (config-node first then the normal ones). When all functions have been called, the runtime triggers `flows:started` event.

For me it sounds more like an initialisation phase - config node cannot be executed but it can be initialised. Once all nodes have been initialised or setup, then the flows:started triggers things like "inject after X seconds" on the inject node.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 19:26 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/11 "2024-01-04T19:26:50Z")

</div>

Sorry, I expressed myself badly (dyslexic too 😅)

What I'm trying to achieve when NR starts is a flow that runs until done is called then stops/freezes then standard flows start. When NR stops, standard flows stop then a final flow starts and executes until done is also called and finally stops and the runtime is stopped.

I don't know if I should use initialize or execute but the node cannot trigger anything until it is in the right phase.

> [@TotallyInformation](#):
>
> And it brings its own set of issues such as startup time.

No really, this is the principle of initialization and shutdown - it is only executed once when the machine starts/stops respectively

---

<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:** [4 January 2024 19:40 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/12 "2024-01-04T19:40:34Z")

</div>

> [@GogoVega](#):
>
> What I'm trying to achieve when NR starts is a flow that runs until done is called then stops/freezes then standard flows start. When NR stops, standard flows stop then a final flow starts and executes until done is also called and finally stops and the runtime is stopped.

This is mostly possible. Even before shutdown - as long as Node-RED actually shuts down, that, of course, is not always the case. Node-RED may simply go away for a number of reasons and in that case, it cannot shut down so you need to take that into account.

But custom nodes would currently be needed to handle this.

> [@GogoVega](#):
>
> I don't know if I should use initialize or execute but the node cannot trigger anything until it is in the right phase.

Nodes are executed/initialised on startup. Specific methods of the Node are then run at different stages. It is fairly trivial to make a node with a specific gate. That gate can easily be set to take a phase as an input not simply an on/off.

If you built Node-RED to have a single entry and exit phase, it would have limited use I think but might have some interest.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 19:53 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/13 "2024-01-04T19:53:31Z")

</div>

> [@TotallyInformation](#):
>
> Node-RED may simply go away for a number of reasons

Yeah, I mean a normal shutdown

So when the runtime starts:

1. Loading all flows
2. Starting the pre-flow
3. pre-flow started
4. done called
5. Stopping the pre-flow (close event)
6. Starting normal flows
7. Flows started =\> NR started

When the runtime stops:

1. Stopping normal flows
2. Starting the post-flow
3. post-flow started
4. done called
5. Stopping the post-flow
6. post-flow stopped =\> NR stopped

---

<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:** [4 January 2024 19:56 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/14 "2024-01-04T19:56:14Z")

</div>

Well, it would be possible. I think we would want to hear from Nick as to whether he thought it a good idea though. Personally, I wouldn't want this to take away from other core development that I think is more critical for change but other than that, I can't see that it should be harmful.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 20:01 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/15 "2024-01-04T20:01:11Z")

</div>

> [@TotallyInformation](#):
>
> I wouldn't want this to take away from other core development that I think is more critical for change

Sorry, what do you mean?

I add that this is an "Industrial" case, even me have no use for it at the moment 😂

---

<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:** [4 January 2024 20:03 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/16 "2024-01-04T20:03:46Z")

</div>

> [@GogoVega](#):
>
> Sorry, what do you mean?

I mean that I wouldn't want people spending time on developing it or having its development distract from other Node-RED releases until other priority developments have taken place. That is simply my opinion, especially given that Node-RED's node.js baseline is already 2 major versions behind.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 20:06 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/17 "2024-01-04T20:06:56Z")

</div>

Of course, this is why I am not making a request but rather seeing if it is possible because it could be interest the industrial world

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [4 January 2024 20:34 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/18 "2024-01-04T20:34:15Z")

</div>

> [@GogoVega](#):
>
> - Loading all flows
> - Starting the pre-flow
> - pre-flow started
> - done called
> - Stopping the pre-flow (close event)
> - Starting normal flows
> - Flows started =\> NR started

I'm sure you're aware of those details:  
No flow was ever loaded at runtime start: It's a number of nodes (scripts) that is loaded.  
No flow was ever started: It's usually one node or more that initiates a sequence of events.  
Currently all nodes are loaded & then sit idle - until an event triggers them.  
Nodes may run logic before they are triggered!  
...

Looks like there's a lot of work to be done to change the current architecture to act in accordance to that pattern.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [4 January 2024 21:41 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/19 "2024-01-04T21:41:13Z")

</div>

> [@ralphwetzel](#):
>
> No flow was ever loaded at runtime start: It's a number of nodes (scripts) that is loaded.

`redNodes.loadFlows()` =\> `storage.getFlows()` =\> Reading the `flows.json` file

> [@ralphwetzel](#):
>
> No flow was ever started: It's usually one node or more that initiates a sequence of events.

`redNodes.startFlows()` =\> `setFlows` =\> `Flow.create()` for each flows then `Flow.start()` each flows.

> [@ralphwetzel](#):
>
> Currently all nodes are loaded & then sit idle - until an event triggers them.

Before starting all the active flows it would be necessary to start only the `pre` flow then the done event triggers the current start of the active flows. The heaviest is this event - that would be something like:

```js
redNodes.loadFlows()
  .then(() => {
    redNodes.startPreFlow()
      .then(() => { redNodes.startFlows() })
      .catch(function(err) {})
  })
  .catch(function(err) {});

```

It will indeed be necessary to add logic but it does not modify all the current logic

---

<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:** [4 January 2024 22:00 UTC](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295/20 "2024-01-04T22:00:52Z")

</div>

What would happen if a message was sent to a non started flow?

[Next page](https://discourse.nodered.org/t/feedback-wanted-flows-sequence/84295.md?page=2)
