Accessing context from .js file

125 views
Skip to first unread message

Patrick Perte

unread,
Sep 20, 2017, 12:44:40 PM9/20/17
to Node-RED
Hi everyone, just had a quick question regarding context. Is it possible for me to access the global context inside the .js file of a node? The reason I'm asking is because I would like to create a node where the user can compare the results of two function nodes, and the only way I can see of doing that is to store the results in the global context, and access them at a later time.

Patrick Perte

unread,
Sep 20, 2017, 12:49:04 PM9/20/17
to Node-RED
I should specify that by .js file I mean the javascript file of the node that is executed when the flow reaches the node, not a text editor within the node that has javascript code in it.

Julian Knight

unread,
Sep 20, 2017, 5:33:00 PM9/20/17
to Node-RED
Hi Patrick, yes you can access both flow and global context variables when creating your own node. You can also create/access the node's own context variables.

Here is a comment block from my test node: https://github.com/TotallyInformation/node-red-contrib-jktesting

// NOTE that this nodes context variables are available from (node.context()).get('varname')
// The node's flow variables are available from (node.context().flow).get('varname')
// The global variables are available from (node.context().global).get('varname')

Nick O'Leary

unread,
Sep 20, 2017, 6:56:09 PM9/20/17
to Node-RED

And here's the proper documentation about it:

https://nodered.org/docs/creating-nodes/context

But I'd like to hear more about what you're trying to do. Have a node examine global context in order to see what Function nodes have set is not a normal pattern. If you can describe a bit more about the scenario you have there may be a more natural approach.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/acb18949-ba43-40cb-9fb5-24e2e5fcec17%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave C-J

unread,
Sep 20, 2017, 7:13:39 PM9/20/17
to node...@googlegroups.com
surely the function nodes just output their results... and you wire hem into your other node - so the data just arrives on.("input"... 

Patrick Perte

unread,
Sep 21, 2017, 4:29:40 PM9/21/17
to Node-RED
Thanks for all the advice everyone. I completely forgot that when a message comes in, I could just do something along the lines of 
this.input[1] = msg.topic
to save the incoming messages, silly me. Don't really think I need to access the context from within the .js files anymore.

Patrick Perte

unread,
Sep 26, 2017, 2:01:37 PM9/26/17
to Node-RED
So after experimenting for almost a week it seems my previous solution does not work. I think I will need to find a way to access the global context from within a node's js file. To answer your question Nick, I'll detail what I'm trying to do.

There are a number of OPC tags that a user can define. This list of tags subscribes to a database to see if any values related to the tags change. Changes to the tags are recorded and the user can define new properties to set to them. Within the flow, the tags have a few default properties set so that  when they are sent back to the database at the end of the flow they can be uploaded correctly. Now, before the user subscribes to the tags, the initial values of the respective tags that are already present in the database are read and stored in the global context of node-red. 

In the flow, custom nodes that I have created can be used to perform specific operations on any tag and their properties that the user wants. The user can specify which tag and which property should be operated on. A few of my custom nodes are forms that replicate simple logic gates (AND, OR, etc.) that compare two properties of two tags and follow a specific output route depending on the answer. For example, if a user wanted to compare tag1.trigger with tag2.trigger, the msg that comes in from subscribe will either only be tag1, tag2, or another tag that has changed. Either way, I need a way to compare the two tags, and with one of them being part of the msg, I am unsure of how to access the second one. The logic of the comparison is handled within the js file, which is why I questioned whether accessing the context within a js file is even possible.

Please let me know if you need clarification as to what I'd like to accomplish. Here's a picture of the flow as a reference:


Julian Knight

unread,
Sep 28, 2017, 4:45:09 AM9/28/17
to Node-RED
Question - are your flows multi-customer/user? That's to say, do you need to track the user as well as the tags? Obviously if you do, the logic is going to be a fair bit more complex.

If each user runs their own flow then that is fairly simple. In that case, it sounds like all you need to do is track changes to the tags between inputs to your nodes? So all you need to do is understand which bits of a custom node are retained between incoming messages. The experimental node I mentioned before may be helpful. I believe you are currently over-thinking the logic - from what I can tell, your requirement isn't quite as complex as you think.

steve rickus

unread,
Sep 28, 2017, 1:41:52 PM9/28/17
to Node-RED
Patrick,

It sounds to me like what you need to build (along with your custom node) is a config node that holds the tag definitions. Then each of your custom nodes would reference that one config node, thereby all sharing the tag definitions. Have you looked at the docs for creating your custom config node?

The config node would hold the params that are used to connect to the database, so each node would not have to know where the tag definitions were stored. Then every time a new custom node was created, it would ask the config node to get the list, including default/current values. I think a good example of this would be how the mysql nodes use a single config node to hold connection and login parameters. Here's the github source:


--
Steve
Reply all
Reply to author
Forward
0 new messages