Working with messages
Dernière mise à jour
Dernière mise à jour
Writing programs in text works, and in most cases it works well. However, the ability to express programs visually is often desirable. Being able to design the flow of information through various components of a larger system is often all that is needed. Visual programming tools are also lenient towards anyone who is new to programming and struggling to handle various concepts like variables, pointers, signals, scopes, and so on.
Messages are what pass between the nodes in a flow. They are plain JavaScript objects that can have any set of properties. They are often referred to as msg
within the editor.
By convention, they have a payload
property containing the most useful information.
The value of a property can be any valid JavaScript type, such as:
Boolean - true
, false
Number - eg 0
, 123.4
String - "hello"
Array - [1,2,3,4]
Object - { "a": 1, "b": 2}
Null
The easiest way to understand the structure of a message is to run a flow and view it in the Properties sidebar.
When displaying an Array or Object, the sidebar provides a structured view that can be used to explore the message.
At the top it shows the name of the property that has been passed in. Here, the default msg.payload
has been used.
Next to the property name is the type of the property - Object
, String
, Array
etc.
It then shows the contents of the property. For Arrays and Objects, the property is collapsed into a single line. By clicking on it, the property will expand to show more detail.
The msg.payload
property is a key part of the message object. It holds the main data or information that a node processes or generates. This can be anything from API readings, user inputs, to computed results.
Nodes in a flow can access and manipulate the msg.payload
property. They can read its current value, modify it, or replace it with new data. This allows for seamless transfer of data between nodes in the flow.
Since msg.payload
can hold any type of data, it provides flexibility in how information is processed within the flow. Nodes can interpret the payload based on its context or purpose, enabling diverse functionalities within the same flow.
msg.payload
is commonly used for debugging and logging purposes. Nodes can output the payload to the debug console or log files, allowing developers to monitor the flow of data and track its transformation during execution.
In event-driven flows, nodes often use msg.payload
to convey event data or signals. For example, a node may generate an event message with relevant information stored in the payload, which triggers subsequent actions in the flow.
Working with JSON
JSON, (JavaScript Object Notation), is a standard way for representing a JavaScript object as a String. It is commonly used by web APIs to return data.
If a message property contains a JSON string it must first be parsed to its equivalent JavaScript object before the properties it contains can be accessed.