Skip to content

Commit a252880

Browse files
committed
Add message design section
1 parent 497ee55 commit a252880

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

docs/developing-flows/index.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@ into smaller, reusable components and how to customise them for different platfo
2828

2929
### [Message design](message-design)
3030

31-
**Not written yet**
32-
33-
- payload as the main content
34-
- properties at top level, vs everything in payload
35-
- Take care of node side-effects (msg.topic for example)
36-
- Use properties to identify the origin of a message
37-
- Preserve message properties
38-
- Cloning messages
31+
This section looks at how the design of messages can help create nodes and flows
32+
that work well together and are easier to maintain.
3933

4034
[more...](message-design)
4135

docs/developing-flows/message-design.md

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,76 @@ properties set on them.
1111
They usually have a payload property - this is the default property that most nodes
1212
will work with.
1313

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+
1422
When creating flows, the choice of properties used on a message will largely
1523
be determined by what the nodes in the flow require.
1624

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+
<div style="width: 300px" class="figure">
30+
<img src="images/placeholder.png" alt="Link nodes">
31+
<p class="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+
<div style="width: 300px" class="figure">
37+
<img src="images/placeholder.png" alt="Link nodes">
38+
<p class="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+
<div style="width: 300px" class="figure">
52+
<img src="images/placeholder.png" alt="Link nodes">
53+
<p class="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.
1782

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`.
1884

1985

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
24-
- Preserve message properties
25-
- Cloning messages
86+
##

0 commit comments

Comments
 (0)