A node red flow consists of a series on interconnected nodes.(wired nodes).
All nodes must have an input and can have 0 or multiple outputs.
Nodes exchange data using the msg object.
Each node receives the message object from the previous node, and then can pass this message object onto the next node in the flow.
The Msg Object
The message object has several properties depending where it originated.
A msg object that originates from an MQTT input has :
You can see the message properties by sending the msg to the debug node.
By default the debug node will display the msg.payload property but you can edit the node to display all message properties.
This is what you see:
Modifying the msg Object
Any node can read or modify the properties of the msg object, and they can also extend the message object.
The main ones are change,split,join,switch.
You can find out more of what they do by dragging them onto the flow and then viewing the info tab.
See the documentation for more details
Using The Function Node with The Msg Object
When you create a new function node and go to edit then you will see a single line of code that returns the msg object and a blank line above were you start to enter your code.
The msg object is passed into this function by the proceeding node which you don’t see ,and the return at the end passes the msg object onto the next node in the flow.
Creating a New Message Object
Inside a function you can create a new msg object using:
However the msg will loose properties of the original message object and may break some flows.
- Github- Node-red Msg Conventions-Reserved Message properties
- Node Red User Guide –Messages
- Node Red User Guide- writing functions
- Node-Red Overview for Beginners
- Storing Data in Node-Red Variables
- Securing Node-Red with SSL and Username Authentication