# Convert to sub-flows

**URL:** <https://discourse.nodered.org/t/convert-to-sub-flows/12169>\
**Category:** General\
**Created:** [13 June 2019 08:09 UTC](https://discourse.nodered.org/t/convert-to-sub-flows/12169 "2019-06-13T08:09:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsenden](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rsenden/32/35728_2.png) [@rsenden](https://discourse.nodered.org/u/rsenden)\
**Post date:** [13 June 2019 08:09 UTC](https://discourse.nodered.org/t/convert-to-sub-flows/12169/1 "2019-06-13T08:09:58Z")

</div>

I have defined the following flow in Node-RED:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/183b1dc9515aac32e3087e121ba965025c32d036.png)

Various flows connect to the `OUT_RFX` input link to send messages to RFXCom devices. Any incoming messages are also replicated to the `OUT_RFX_RECEIVED` output link to allow other flows to be notified. For example, I use this to update dashboard switches, or to send a Telegram message to my Telegram debugging channel.

Is it possible to convert this functionality to sub-flows like the following?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/985f01ed45e806031b545460d795d1da8dcf2810.png)

Basically the `RFX Out` sub-flow would process any incoming message in the same way as my current flow, but it would also need to forward the message to all instances of the `RFX Out Received` sub-flow.

It seems like link nodes cannot be used in sub-flows, otherwise I could simply convert my current flow to the `RFX Out` sub-flow, and connect the `OUT_RFX_RECEIVED` link to an input link defined in the the `RFX Out Received` sub-flow.

I guess I could add a function node that uses the Node-RED API to find all `RFX Out Received` nodes and send the incoming message to each of these nodes. Does anyone have any examples on the API functions to use, or are there any better ways of accomplishing this?

---

<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:** [13 June 2019 08:33 UTC](https://discourse.nodered.org/t/convert-to-sub-flows/12169/2 "2019-06-13T08:33:22Z")

</div>

Whats the main reason you want to wrap that flow into a subflow? Is it just to organise your flow by hiding its internal details, or do you want to have multiple instances of the subflow?

You are right that you cannot have link nodes going in or out of a subflow. You could put the 'internal' nodes of your flow into a subflow and just keep the link nodes outside:

```auto
[OUT_RFX] -> [subflow] -> [OUT_RFX_RECEIVED]

```

> [@rsenden](#):
>
> I guess I could add a function node that uses the Node-RED API to find all `RFX Out Received` nodes and send the incoming message to each of these nodes. Does anyone have any examples on the API functions to use, or are there any better ways of accomplishing this?

No. You cannot do that. The Function node does not have access to the internal APIs of the runtime.

---

<div class="post-metadata">

**Author:** ![rsenden](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rsenden/32/35728_2.png) [@rsenden](https://discourse.nodered.org/u/rsenden)\
**Post date:** [13 June 2019 10:56 UTC](https://discourse.nodered.org/t/convert-to-sub-flows/12169/3 "2019-06-13T10:56:43Z")

</div>

Thanks again for the quick reply!

> [@knolleary](#):
>
> Whats the main reason you want to wrap that flow into a subflow?

As an example, previously I had a similar `RFX In` flow, which receives messages from the RFXCom device, performs some operations on the message, and then forwards the message to an `IN_RFX` link. All functional flows that need to be triggered by RFXCom input messages then connected to this link.

I have now converted this re-usable flow into an `RFX In` sub-flow. All functional flows that previously connected to the `IN_RFX` link now connect directly to the `RFX In` sub-flow. This results in various advantages:

1. Hide internal nodes.
2. Reduction of the number of links. Especially when configuring a link in or link out node, it becomes difficult and error-prone to select the correct link(s) in a large list of links.
3. Re-usable sub-flows usually don't need a lot of maintenance, so the ability to close these tabs makes it easier to focus on functional flow tabs. (note that I currently have a separate flow tab for each reusable flow)
4. A sub-flow node provides a default name, description and icon, making it much easier to understand the flows that use these sub-flow nodes. In comparison, a link node by default only provides a generic link icon. You need to update name/icon/description/'show label' settings for every individual link to have it appear similar to a sub-flow node, and/or click on the link node to see where it connects to.

> [@knolleary](#):
>
> You are right that you cannot have link nodes going in or out of a subflow. You could put the 'internal' nodes of your flow into a subflow and just keep the link nodes outside:
> 
> ```auto
> [OUT_RFX] -> [subflow] -> [OUT_RFX_RECEIVED]
> 
> ```

Taking this a bit further, given the flow simplification due to using a sub-flow like this, it would become viable to move all reusable flows to a single `Reusable Links` tab (instead of having a separate tab for every reusable flow). Comparing to the list of sub-flow advantages above:

1. Hides internal nodes (except for the link nodes).
2. Given that all re-usable links are on a single tab makes it a little easier to connect to the correct link when adding a new link, as the link settings page organizes links by tab.
3. The `Reusable Links` tab cannot be closed, but still better than having multiple tabs with reusable flows that cannot be closed.
4. This would still be an issue. I guess though I could add corresponding links on either the `Reusable Links` tab or in my library (haven't used the library before) with correct label/description/icon, and then copy/import such links from the library when I need to connect to these reusable flows. For example, for the `OUT_RFX` input link, I could define a corresponding `OUT_RFX` output link that can then be copied/imported to the flows that need to send messages to RFXCom devices.

Another minor issue with this approach is consistency; some flows like `RFX In` can be fully converted to a sub-flow, whereas other flows like `RFX Out` need to referenced through links.

I think I'll use this approach for now, but I would still be interested in potentially better approaches.
