A simple caching node that works with uibuilder.
See Documentation: Using the cache node for more detail.
Inputs
Any msg can be sent into the node.
In addition, the node recognises uibuilder control messages for cache replay and cache clear.
See Documentation: pre-defined messages
for the structure of those messages.
Outputs
Any non-uibuilder-control message is passed to the output.
If the node receives a REPLAY or "client connect" (where the connections count is zero) control message, all of the cached messages are sent to the output.
Upon replay, the output messages will have the socket id of the control message (if present) added
so that the replayed messages are sent to the correct client. Any socket id saved in the cached messages
is ignored.
Upon replay, the output messages will also get a _uib.cache
property added with a value of "REPLAY".
This can be used in your front-end code if you need to differentiate between original and replayed messages.
If the node receives a CLEAR control message, all of the cached messages are deleted and nothing is sent to the output.
Node Settings
- Cache by string
-
Enter an incoming message property to group cached entries by.
When choosing a property to group by, be mindful of the number of messages
that may end up in the cache. If too many large messages are cached, Node-RED WILL crash.
The default entry is "topic".
- Cache individually? boolean
-
If selected, the "Cache by" field will be hidden. All messages will be
cached as a single group.
- Only replay cache when client is really new boolean
-
If selected, when a "client connect" control message is received, the cache will only
be sent if
msg.connections
is zero. That indicates that the client connection
is actually a new one and node a reconnection. This is now the default as it reduces the number
of times the cache is sent and does not send if the client is simply waking up (when it doesn't need the cache.)
- # messages integer
-
The maximum number of messages per group that will be cached.
Once the max number is reached, the oldest messages will be dropped.
If you change this number and re-deploy, the node will automatically trim
the number of messages cached.
You can use a number of zero to allow an infinite number of cached entries,
however, you need to take great care in that case not to exceede the available
memory, the node will not check for you.
The default number is one. This means that the last recorded message for each group
will be replayed which is generally the expected result.
- Use Store Drop-down
-
The drop-down is pre-populated with the
contextStorage
modules you define in settings.js
.
You can use this to choose a persistent context store if desired so that the cache survives system restarts.
If you change this value, you may need to manually delete the old version of the node context variable to avoid confusion.
Select the node, view the Context Data side-panel, refresh the entries and delete the appropriate uib_cache
entry.
- Store Type Drop-down
-
One of "Node", "Flow", or "Global".
Take care if using "Flow" or "Global" since other nodes and flow might make changes to the cache.
"Node" is the default and should usually be used since there can be no name clashes or external influence.
- Variable string
-
Default: "uib_cache". This is the store variable name that will be used.
Take care if using flow/global stores and multiple cache nodes since a name clash will result in the same cache being used.
- Name string
- A short description shown in the admin interface.
NOTE that there is an example flow in the examples library that demonstrates the use of this node.
Use the Editor's menu, import, Examples, node-red-contrib-uibuilder and import the uib-cache-test
example.