Multiple Outputs With Labels for Function Nodes in Dialogue Studio
In complex Dialogue Studio (Node-RED) flows, managing multiple branches and outcomes can become a daunting task. Normally we can use the switch node to evaluate outputs and branch the flow into different paths. While effective and easy to manage for non-developers, this method could make flows quite busy, especially when dealing with multiple conditions.
A way to limit the number of nodes and streamline your workflow is by using a function node that has multiple outputs with labels. This approach effectively combines the switch node and the function node, reducing clutter and increasing readability.
Here’s an example to handle three different HTTP response statuses:
if (msg.statusCode === 200) {
msg.payload = "Success";
return [msg, null, null]; // Output 1: Success, Outputs 2 and 3: null
}
if (msg.statusCode === 404) {
msg.payload = "Not Found";
return [null, msg, null]; // Output 2: Not Found, Outputs 1 and 3: null
}
msg.payload = "Other Status";
return [null, null, msg]; // Output 3: Other Status, Outputs 1 and 2: null
There’s an even more streamlined way to handle cases as the outputs that are null
and are after the one that is not null
can be omitted. This simplifies the code further.
Here’s an example that highlights this approach:
if (msg.statusCode === 200) {
msg.payload = "Success";
return [msg]; // Output 1: Success, Outputs 2 and 3: null (omitted)
}
if (msg.statusCode === 404) {
msg.payload = "Not Found";
return [null, msg]; // Output 2: Not Found, Output 3: null (omitted)
}
msg.payload = "Other Status";
return [null, null, msg]; // Output 3: Other Status, Outputs 1 and 2: null
You can of course re-use any other output for the default case, or not have a default case at all. It is all up to you.
Setting Up Labels for Outputs:
In Node-RED, you can also label these outputs for better clarity which effectively replicates what the switch node does.
- Setup Tab: Define the number of outputs to match the function code (in this case, three).
2. Appearance Tab: Label each output, such as:
- Output 1: “200: Success”
- Output 2: “404: Not Found”
- Output 3: “Other Status”
You can then see the labels on hovering over them which allows for quick understanding of the flow without the need to dive into the code itself.
I hope this will help you reduce cluter and make your flows, well, flow.
More infomration on Anywhere365 Dialogue Studio can be found here: https://golive.anywhere365.io/platform_elements/dialogue_studio/dialogue-studio-overview.html
Node-RED user guide on functions and multiple messages: https://nodered.org/docs/user-guide/writing-functions#multiple-messages
Node-RED user guide on port labels: https://nodered.org/docs/creating-nodes/appearance#port-labels