You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/developing-flows/message-design.md
+67-6Lines changed: 67 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -11,15 +11,76 @@ properties set on them.
11
11
They usually have a payload property - this is the default property that most nodes
12
12
will work with.
13
13
14
+
For more information about messages in Node-RED you should read the [Working with messages](/docs/user-guide/messages)
15
+
section of the user guide.
16
+
17
+
This section looks at some of the choices you need to make when deciding how to
18
+
stucture the messages in your flows.
19
+
20
+
### Working with the payload
21
+
14
22
When creating flows, the choice of properties used on a message will largely
15
23
be determined by what the nodes in the flow require.
16
24
25
+
Most nodes will expect to work with `msg.payload` and that will guide most of the choices you make.
26
+
27
+
For example, consider a flow that receives an id in the payload of an MQTT message. It then uses that id to query a database to find a matching record.
28
+
29
+
<divstyle="width: 300px"class="figure">
30
+
<imgsrc="images/placeholder.png"alt="Link nodes">
31
+
<pclass="caption">Placeholder for image: example mqtt to database query flow</p>
32
+
</div>
33
+
34
+
The database node will put its result in the payload of the message it sends - overwritting the original id value. If the flow needs to be able to reference that id value later on, it can use a Change node to copy the value to another property that will not get overwritten.
35
+
36
+
<divstyle="width: 300px"class="figure">
37
+
<imgsrc="images/placeholder.png"alt="Link nodes">
38
+
<pclass="caption">Placeholder for image: example change node to save msg.id</p>
39
+
</div>
40
+
41
+
42
+
This highlights an important principle: nodes should not modify or remove properties on messages that are not related to their functionality.
43
+
44
+
For example, in most cases, a Function node should send on the same message object it received rather than create a new message object.
45
+
46
+
47
+
### Using `msg.topic`
48
+
49
+
A number of nodes also treat `msg.topic` as having special meaning. It might be used to identify the source of the message, or to identify different 'streams' of messages on the same flows. It also gets displayed in the Debug sidebar with every message.
50
+
51
+
<divstyle="width: 300px"class="figure">
52
+
<imgsrc="images/placeholder.png"alt="Link nodes">
53
+
<pclass="caption">Placeholder for image: topic in debug sidebar message</p>
54
+
</div>
55
+
56
+
For example, the MQTT In node will set `msg.topic` to topic the message was received on. The Delay node can then be configured to rate limit messages according to their topic.
57
+
58
+
Whilst your flow may not use nodes that depend on `msg.topic` directly, it can be used to give extra contextual information about a message. But you should take care if you later introduce nodes to the flow that do depend on its value.
59
+
60
+
61
+
### Designing message properties
62
+
63
+
When designing a node or subflow for reuse, the message properties it works with and the properties it sets are all part of the API it exposes. As with all APIs, it needs to be designed with care and attention. This applies to flows as well.
64
+
65
+
One approach is to put everything under the payload. For example:
66
+
67
+
```json
68
+
{
69
+
"payload": {
70
+
"temperature": 123,
71
+
"humidity": 50,
72
+
"pressure": 900
73
+
}
74
+
}
75
+
```
76
+
77
+
This may be convenient to keep the data together, but it can also lead to a lot of moving properties around as later nodes expect to operate on `msg.payload` and not a property underneath it.
78
+
79
+
A different approach, as seen by the Twitter node, is to put the most 'interesting' information into the payload, in this case the text of a tweet, and put the complete metadata the API also provides into a separate `msg.tweet` property.
80
+
81
+
There is not a single right answer to how to structure the message, but it should focus on how the node or flow is going to be used in the most common cases.
17
82
83
+
As with programming in general, the choice of good property names is also important. They should be self-describing to help with later debugging and understanding of the flow. For example, `msg.temperature` is much more understandable than `msg.t`.
18
84
19
85
20
-
- payload as the main content
21
-
- properties at top level, vs everything in payload
22
-
- Take care of node side-effects (msg.topic for example)
23
-
- Use properties to identify the origin of a message
0 commit comments