Get node instance via RED.nodes.getNode


Hi folks,

Feels like I'm developing my first function node...
I created a simple function node that counts the input messages:


[{"id":"ec7fdd88.78f28","type":"function","z":"5a89baed.89e9c4","name":"msg counter","func":"// First time initialization\nif (!this.msgCounter) {\n    this.msgCounter = 0;\n}\n\nthis.msgCounter = this.msgCounter + 1;\n//node.msgCounter++;\n\nmsg.msgCounter = this.msgCounter;\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":160,"wires":[["ff8f2dc7.37996"]]},{"id":"33e53ca0.90be34","type":"inject","z":"5a89baed.89e9c4","name":"Inject msg","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":160,"wires":[["ec7fdd88.78f28"]]},{"id":"ff8f2dc7.37996","type":"debug","z":"5a89baed.89e9c4","name":"Show output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"msgCounter","x":770,"y":160,"wires":[]}]

And that works fine: the counter increments for every message. Otherwise I would have stopped developing in Node-RED right away :wink:

However in the function module I store the counter value in this.msgCounter:

this.msgCounter = this.msgCounter + 1;

So I don't store the counter on the node context memory, as I normally do. But when I try to find the function node, based on it's node id:

var test = RED.nodes.getNode("ec7fdd88.78f28");

Then I get an instance of the function node without 'msgCounter' property:

Can anybody explain this? And does anybody know a solution to get the instance that has the 'msgCounter' property?

Thanks !!


Hi Bart. What happens if you define a proper node context variable and do the standard get/modify/set sequence? Just guessing here, but I suspect that your function node doesn't really "own" the msgCounter property -- it belongs to the calling function, probably the runtime. But don't trust me on this.


the function runs in a sandbox - so the "this" refers to the inside of the sandbox.


Hey Mike (@drmibell), normally I would do it like that but I was experimenting for a new contribution that determines the memory usage of selected nodes. If a function node stores it's data on 'this', then the size of that data should also become visible via the new contribution. But it doesn't at the moment :woozy_face:

Hi Dave (@dceejay), that indeed makes sense. But that suddenly turns a function node already into an exception, when I want to determine the memory size of each node. Damn that makes it again more complex, especially since there are other nodes that use sandboxes. I assume there is no easy way to get hold of the sandbox and its data ...


Hey Dave (@dceejay),

Tried to get hold of the sandbox, to check whether I could determine somehow the amount of memory being used ...

But no luck. When I look at the code of the function node, it seems to me that a reference to the sandbox instance is being stored inside this function node:

this.script = vm.createScript(functionText, {

But there is no script property on the function node, when I call RED.nodes.getNode (as you can see in the screenshot of my original post above)? Or am I completely missing something fundamental here?

P.S. Don't want to give up too soon, but I'm getting the impression that it is almost impossible to track the memory usage of individual nodes in a flow ...


I can see the script property in your screenshot above.

Yes. As I've said before, you can only examine the objects the node stores as properties on itself. There is no guarantee that a node will do that. The function node could just as easily do:

var script = ....

And the node would still be able to access it as it is in scope, but it isn't stored as a property of the node.

1 Like