|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +toc: practice-toc.html |
| 4 | +title: Designing a flow |
| 5 | +--- |
| 6 | + |
| 7 | +### Development steps |
| 8 | + |
| 9 | +*If a project needs complicated logic, it is better to design flow before starting development. After that, you create flows based on the flow design. In this subsection, we show an overview of whole recommended steps of design and development.* |
| 10 | + |
| 11 | +* *Design* |
| 12 | + - *Flow structure* |
| 13 | + - *Message* |
| 14 | +* *Implementation* |
| 15 | + |
| 16 | +### Designing flow structure |
| 17 | + |
| 18 | +*As the size of flows in a tab gets larger, it can be difficult to recognize a function of each flow within a tab. To improve reusability of flows, it would be better to decide the responsibility of flows within each tab and the scale of one tab. To do this, it is helpful to sort out what kind of things, people, work, rules are in the target domain of the application, and relationship of them.* |
| 19 | + |
| 20 | +### Designing messages |
| 21 | + |
| 22 | +*There are risks that multiple nodes have dependencies by messages passing through nodes.* |
| 23 | +*For other developers to reuse flows, it is important to design messages so that dependencies get to be relaxed. This chapter proposes a guide about designing message.* |
| 24 | + |
| 25 | +*We have already written actual contents on [here](https://github.com/node-red/node-red.github.io/wiki/Flow-Guideline-Contents).* |
| 26 | + |
| 27 | +#### Designing key-value structure of `msg` |
| 28 | + |
| 29 | +*`msg` is a JavaScript object that contains a key-value structure like JSON. While a `msg` transits across multiple nodes, the nodes use some keys and values of the `msg`. If two or more nodes of them use the same key for different their own purposes, preparing `msg` for input of the nodes is so difficult.* |
| 30 | + |
| 31 | +*Therefore, policies of key-value structure are needed and this subsection describes it as followings,* |
| 32 | + |
| 33 | +* *Top-level keys of `msg` are used to control the functionality of node* |
| 34 | +* *`msg.payload` is used as input parameters of a process of a node* |
| 35 | + |
| 36 | +#### Keeping properties |
| 37 | + |
| 38 | +*In the case of using function node, you can prepare output messages by creating new `msg` objects. However, the output messages may not have some properties that the input message has. This means that properties that should be kept in your flow has lost. Since this can be a cause of serious bugs, preparing output messages based on an input message is better, instead of creating new `msg`.* |
| 39 | + |
| 40 | +#### Add tag into `msg` to distinguish a node that sent the `msg` |
| 41 | + |
| 42 | +*[Tips] If it is needed that a (latter) node executes a different process depending on a (former) node that send `msg`, former node adds tags describing the former node itself. With the tag, the latter node decide the process to execute.* |
| 43 | + |
| 44 | +#### Using persistent storage outside of Node-RED |
| 45 | + |
| 46 | +*If you handle the large amount of data, it is **not** recommended to set the data into `msg` since `msg` can exhaust available memory. Instead, you had better put the data on a persistent storage that is outside of Node-RED and use reference to the data for handling the data.* |
| 47 | + |
| 48 | +#### Processing messages in order of their arrival |
| 49 | + |
| 50 | +*Since Node-RED (JavaScript) processes asynchronously, a node cannot assume that it executes process for arrival `msgs` by the order of arrival.* |
| 51 | + |
| 52 | +<!-- |
| 53 | +If you want to assume processes by the order of arrival, try this code. |
| 54 | + |
| 55 | +```javascript |
| 56 | +// Accumulation of messages |
| 57 | +var msgs = context.get('messages') || []; |
| 58 | +msgs.push(msg); |
| 59 | +if(msgs.length === ...) { |
| 60 | + ... // Process messages |
| 61 | +} |
| 62 | +context.set('messages', msgs); |
| 63 | + |
| 64 | +``` |
| 65 | + --> |
0 commit comments