| layout | docs |
|---|---|
| toc | user-guide-toc.html |
| title | Working with context |
- What is context
- Context scopes
- Context stores
- Using context in a flow
- Using context in a Function node
- Using context in a custom node
Node-RED provides a way to store information that can be shared between different nodes without using the messages that pass through a flow. This is called 'context'.
The 'scope' of a particular context value determines who it is shared with. There are three context scope levels:
- Node - only visible to the node that set the value
- Flow - visible to all nodes on the same flow (or tab in the editor)
- Global - visible to all nodes
The choice of scope for any particular value will depend on how it is being used.
If a value only needs to be accessed by a single node, such as a Function node, then Node context is sufficient.
More often context allows some sort of state to be shared between multiple nodes. For example, a sensor may publish new values regularly in one flow and you want to create a separate HTTP triggered flow to return the most recent value. By storing the sensor reading in context it is then available for the HTTP flow to return.
The Global context can be preconfigured with values using the functionGlobalContext
property in the settings file.
By default, context is stored in memory only. This means its contents are cleared whenever Node-RED restarts. With the 0.19 release, it is possible to configure Node-RED to save context data so it is available across restarts.
The contextStorage property in settings.js can be used to configure how context
data is stored.
For example, to enable file-based storage, the following options can be used:
{% highlight javascript %} contextStorage: { default: { module: "localfilesystem" } } {% endhighlight %}
Node-RED provides two built-in store modules: memory and localfilesystem. It is also
possible to create custom store plugins.
Full details on the built-in modules, and how to create custom modules, is available on the api pages.
Stores can provide either synchronous or asynchronous access. Synchronous access means a call to get data from the store returns immediately with the value. Asynchronous access means a call to get data must also provide a callback function that is called once the value is available.
The built-in memory and file stores both offer synchronous access. This means
existing (pre-0.19) flows can use these stores without any changes.
The easiest way to set a value in context is to use the Change node. For example,
the following Change node rule will store the value of msg.payload in flow context
under the key of myData.
Various nodes can access context directly. For example, the Inject node can be configured to inject a context value and the Switch node can route messages based on a value stored in context.
The Writing Functions guide describes how to use context in the Function node.
The Creating Nodes guide describes how to use context in a custom node.
