GLOBAL FUNCTIONS
Creating global functions in Node-RED can be very useful for reusing code across different flows. Here’s a step-by-step guide on how to create and use global functions:
1. Setting a Global Function
To set a global function, you usually do this in a function
node. This function will then be available in any other function
node in your Node-RED environment.
Example:
Let’s create a simple global function that multiplies a number by 2.
-
Add a
function
node to your flow. - Edit the function node and add the following code:
// Define the function
function multiplyByTwo(x) {
return x * 2;
}
// Set the function globally
global.set("multiplyByTwo", multiplyByTwo);
// Pass on the msg object to the next node (optional)
return msg;
- Deploy the flow.
WARNING: THE FUNCTION NODE CONTAINING THE GLOBAL FUNCTION DEFINITION MUST BE DEPLOYD BEFORE THE FIRST USAGE FROM AN EXTERNAL NODE . YOU CAN USE AN INJECT NODE CONNECTED TO THE DEFINITION FUNCTION NODE.
This code defines a function multiplyByTwo
and sets it as a global function using global.set
.
2. Using the Global Function
To use this function in another node:
-
Add another
function
node where you want to use the global function. - Edit the function node and use the global function. For example:
// Get the global function
var multiplyByTwo = global.get("multiplyByTwo");
// Use the function
msg.payload = multiplyByTwo(msg.payload);
// Return the msg object
return msg;
- Connect this node to the rest of your flow as needed.
- Deploy the flow.
This code retrieves the global function with global.get
and then applies it to the msg.payload
.
Tips
- Modularity: Keep your global functions simple and focused on a single task.
- Naming Conventions: Use clear and descriptive names for your global functions to avoid confusion.
- Error Handling: Consider adding error handling within your global functions for robustness.
Using global functions like this helps keep your Node-RED flows more organized and avoids the repetition of code.
GLOBAL FUNCTIONS VS SUBFLOWS
Global Functions
- Definition: A global function is a JavaScript function defined in a Node-RED function node and made available to all other function nodes via the global context.
- Use Case: Best for reusable code logic, especially small utility functions that perform specific tasks (e.g., data transformation, calculations).
- Advantages:
- Simplicity: Ideal for small, simple tasks.
- Portability: Can be used across different flows and instances.
- Flexibility: Being JavaScript, it offers the full power of a programming language.
- Disadvantages:
- Complexity: Not suitable for complex tasks involving multiple steps or nodes.
- Dependency: The function must be defined before it can be used elsewhere in the flow.
- Example: A global function to format a date or calculate a value.
Subflows
- Definition: A subflow in Node-RED is a collection of nodes that are grouped together to create a reusable flow. It can be instantiated multiple times within a project.
- Use Case: Ideal for complex tasks that involve multiple steps or a series of nodes, like processing a particular type of input data or interacting with specific APIs.
- Advantages:
- Modularity: Encapsulates a sequence of operations into a single, reusable node.
- Ease of Use: Drag and drop the subflow into your flow, just like any other node.
- Scalability: Better for complex tasks that require multiple nodes.
- Disadvantages:
- Overhead: More overhead than a simple function for small tasks.
- Limited Flexibility: You’re limited to the capabilities of the nodes within Node-RED (though this can be extensive).
- Example: A subflow for processing incoming sensor data, including parsing, error checking, and logging.
Summary
- Global Functions are best for simple, repeatable code tasks that can be encapsulated in a JavaScript function.
- Subflows are more suitable for complex tasks that require multiple nodes and steps.
In practice, a well-organized Node-RED project might use both methods: global functions for utility tasks and subflows for complex processes. The choice depends on the specific requirements of the task at hand and the complexity involved.