Accessing and manipulating data stored in context().global from nodejs modules

Hi, I have a question regarding accessing and manipulating data stored in context().global from nodejs modules.

For example a node “15virtual-device-start.js” node-red-contrib-iot-virtual-device/15virtual-device-start.js at master · ibm-watson-iot/node-red-contrib-iot-virtual-device · GitHub, uses

var deviceManager = require('./lib/deviceManager.js');
deviceManager(RED);

and “deviceManager.js” node-red-contrib-iot-virtual-device/deviceManager.js at master · ibm-watson-iot/node-red-contrib-iot-virtual-device · GitHub, uses cache object
cache = {};

How to store this "cache" in context().global to be accessed by other nodes, instead in memory???

Regards
Tomasz

Hi Tomas, welcome to the forum.

The documentation for this is here: Node context : Node-RED (nodered.org)

const globalContext = this.context().global

globalContext.set('storedCache', cache)

Just be aware however that there is no guarentee that a context store will store the data directly, some store types have to stringify the data on store and JSON.parse it on recovery. While this normally works just fine, it can occasionally catch you out because there are some things that don't translate correctly.

ok, but where in deviceManager.js should I put:

const globalContext = this.context().global
globalContext.set('storedCache', cache)

if I put it in "init()" I get TypeError: this.context is not a function (line:20)

How can I refer to context().global from external nodejs module?
Is there anything like RED.*.context().global ?

I don't know that node. But I can see that deviceManager.js is a library module and so will not have the correct this context. this in the referenced docs refers to a runtime node instance. You would have to pass a reference to that into the library function.

Thanks!
and where can I find Node-RED runtime API specifications for RED.* ???
Is there any method to examine running instances RED.nodes visually from dashboard?

There is not a lot of good documentation for the internals beyond those APIs nodes are expected to use.

If you have specific questions, I'm happy to help try to point you in the right direction. But we do need to good description of what you're trying to do.