Scope of flow variables in sub-flows

1,036 views
Skip to first unread message

Anthony McDonald

unread,
Nov 18, 2016, 6:26:52 AM11/18/16
to Node-RED
It seems that Flow variables are not properly shared between the "Main" flow and subflows. I can't find any documentation that describes if a flow variable should be available to a sub flow? Should it? I can see arguments on both sides. Perhaps I need a new type of shared variable between Flow and Global?

In the meantime, is there a technique I can use to set a variable in a subflow and access it from the main flow and vice versa? I can't use Global, as the whole point of subflows are that they are reusable by other main flows.


Nicholas O'Leary

unread,
Nov 18, 2016, 6:38:24 AM11/18/16
to Node-RED Mailing List
Don't currently have an answer for this - its a tricky problem to solve.
The challenge is deciding how best to allow the main flow reference the subflow's context, or vice versa. And that depends on the specific scenario you're trying to build.

If a subflow instance could access the 'parent' flow's context, it solves some problems, but doesn't solve everything as you could have multiple instances in the same parent flow. In which case you'd have the same issues as if you were using global - clashing names.

If there was a way for the parent flow to dig into the subflow instance's context, then that would help. But the challenge is how to do that; each subflow instance has a unique id which could be used to reference its context... but then the developer would have to know what those ids were and it would potentially be brittle as you copy and pasted nodes around (generating new ids along the way).

The workaround is to pass parent context variables into and out of the subflow by way of the messages you pass in and send out. Use a Change node on either side of the subflow instance to retrieve the values you want from flow context and attach them to the message, and vice versa on the output of the subflow.

Nick



On 18 November 2016 at 11:26, Anthony McDonald <ajr.mc...@gmail.com> wrote:
It seems that Flow variables are not properly shared between the "Main" flow and subflows. I can't find any documentation that describes if a flow variable should be available to a sub flow? Should it? I can see arguments on both sides. Perhaps I need a new type of shared variable between Flow and Global?

In the meantime, is there a technique I can use to set a variable in a subflow and access it from the main flow and vice versa? I can't use Global, as the whole point of subflows are that they are reusable by other main flows.


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/37ae8e90-0df0-4949-82c7-7632b42c3d34%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony McDonald

unread,
Nov 18, 2016, 6:59:43 AM11/18/16
to Node-RED
Nick, thanks for the fast reply. I can use your workaround. My problem was that as I developed a long flow, I started to break it into reusable subflows, and that broke the chain of using flow variables. I started to use flow variables, as the Dashboard Slider node doesn't copy the original message as it is adjusted, so nodes further down the chain lost the original message. Anthony


On Friday, 18 November 2016 11:38:24 UTC, Nick O'Leary wrote:
Don't currently have an answer for this - its a tricky problem to solve.
The challenge is deciding how best to allow the main flow reference the subflow's context, or vice versa. And that depends on the specific scenario you're trying to build.

If a subflow instance could access the 'parent' flow's context, it solves some problems, but doesn't solve everything as you could have multiple instances in the same parent flow. In which case you'd have the same issues as if you were using global - clashing names.

If there was a way for the parent flow to dig into the subflow instance's context, then that would help. But the challenge is how to do that; each subflow instance has a unique id which could be used to reference its context... but then the developer would have to know what those ids were and it would potentially be brittle as you copy and pasted nodes around (generating new ids along the way).

The workaround is to pass parent context variables into and out of the subflow by way of the messages you pass in and send out. Use a Change node on either side of the subflow instance to retrieve the values you want from flow context and attach them to the message, and vice versa on the output of the subflow.

Nick


On 18 November 2016 at 11:26, Anthony McDonald <ajr.mc...@gmail.com> wrote:
It seems that Flow variables are not properly shared between the "Main" flow and subflows. I can't find any documentation that describes if a flow variable should be available to a sub flow? Should it? I can see arguments on both sides. Perhaps I need a new type of shared variable between Flow and Global?

In the meantime, is there a technique I can use to set a variable in a subflow and access it from the main flow and vice versa? I can't use Global, as the whole point of subflows are that they are reusable by other main flows.


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Dave C-J

unread,
Nov 18, 2016, 8:44:58 AM11/18/16
to node...@googlegroups.com
the slider doesn't keep the original message ? well... it does if the payload is different to the one already set... Most of the UI nodes only send something if the payload changes. and drop anything if they don't. 

Simon Walters

unread,
Mar 11, 2018, 5:04:49 PM3/11/18
to Node-RED
I was going to ask about scope of flow variables and found this thread.

Having read it, am I right in thinking that if i define/use say flow.test in a subflow, then it will be local to that subflow and not interfere with a flow.test in the main flow I use it in?  (Its the behaviour I'm hoping for BTW)


Nick O'Leary

unread,
Mar 11, 2018, 7:19:51 PM3/11/18
to Node-RED Mailing List
Simon - correct, a subflow's flow context is its own and, as per the thread, it cannot directly access its 'parents' flow context.

Nick

On 11 March 2018 at 21:04, 'Simon Walters' via Node-RED <node...@googlegroups.com> wrote:
I was going to ask about scope of flow variables and found this thread.

Having read it, am I right in thinking that if i define/use say flow.test in a subflow, then it will be local to that subflow and not interfere with a flow.test in the main flow I use it in?  (Its the behaviour I'm hoping for BTW)


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages