Storing Data: Getting Started with Node-RED
Node-RED is one of the easiest ways to program ever created but everyone needs a little help
It's quite straightforward to pass plenty of useful data with each message (msg) in your flows. Not only can you store information in msg.payload, but you can also place information in any other named object, for instance, msg.store.
In this article, we will explore some of the better solutions for storing and retrieving transactional information in your flows.
# Storing data outside of msg.payload
In this example, we have data in msg.payload as well as in msg.later.
If you want your debug to display the full content of the message, change the output to 'complete message object' as shown above.
You can import the flow using this code.
Storing data outside of msg.payload can be very useful when you need access to that data later in your flow. You may notice that many nodes overwrite the content of msg.payload, so putting your data elsewhere is essential otherwise, it will be overwritten and lost.
# Tidying your messages
You may also want to remove data you don't need from your messages to optimize the speed of your flows.
It's easy enough to remove data you don't need, wherever it sits within your messages using the Change Node. In this example, we are going to delete the content of msg.other while leaving the rest of the message to be passed to the next Node.
The Change Node is configured as follows.
You can import the flow using this code.
# Storing data outside of msg.payload so you can access it later in your flows
Storing data outside of msg.payload allows you to access it later in your flows. In this example, we inject geographical coordinates and use an API to get the sunset time for each location. We can then output the result as a sentence.
As the HTTP Node, which we are using to interact with the weather API, overwrites msg.payload with the response, we will store the submitted city name and coordinates in msg.store for later use.
You can see the flow working below.
You can import the flow using this code.
Using this technique, we can build the content of message.store (or any other name you'd like to use) and then output the content at the end of a flow, even if you use msg.payload to interact with APIs and other custom nodes.
# Why not store the values in context?
Where possible, it's more robust to store all the information related to a particular message within the message rather than saving it to context and retrieving it later. Using context risks a race condition within your flow that could result in data corruption.
In this example, we simulate how a race condition can make context a bad choice for transactional data storage. The flow passes in the name and age of two people then moves the age to context. The flow then adds a random delay for each message so that, in some cases, the messages do not reach the debug in the order they were created. After the delay, the age is pulled back from context and added to each msg.payload.
If there is a race condition in play, we should intermittently see Rob and John's stored ages being assigned to the wrong person. We can see in the image above that Rob is showing the incorrect age.
You can import the flow using this code.
# Conclusion
Storing transactional data in msg rather than context in Node-RED offers advantages in modularity, scalability, simplicity, and also helps prevent race conditions. By using msg, data transfer between nodes becomes more seamless, allowing for independent node reuse across flows without additional configuration. This approach ensures scalability by avoiding the imposition of large datasets on the global context. Moreover, storing data outside of msg.payload within the msg object enhances flexibility. It separates metadata and other relevant information from the main payload, promoting a cleaner and more organized structure. This practice not only aligns with Node-RED's visual programming paradigm but also improves code readability. Steering clear of context for transactional data, while also organizing it within the msg object, provides a comprehensive and reliable solution within the Node-RED framework.
Thanks to SunriseSunset for creating the useful API we've used in this article.
Written By:
Published on:
Related Articles:
- Capture Data from edge devices with Node-RED
- Custom Vuetify components for Dashboard 2.0
- Node-RED Tips - Dashboard Edition
- Persisting chart data in Node-RED Dashboard 1
- Best Practices Integrating a Modbus Device With Node-RED