Search code examples
node-red

Node-RED: where do I find docs for RED.nodes.createNode/getNode/eachNode/


I may be blind, but I cannot find documentation for functions I've seen in many node implementations as there are:

  • RED.nodes.createNode()
  • RED.nodes.getNode()
  • RED.nodes.eachNode()
  • RED.nodes.originaFlow()

including important details concerning nodes shown in the editor and those actually used by the runtime.

Everything I found seems to be horribly outdated.

For that reason: where do I find up-to-date docs for the mentioned functions?


Solution

  • Just to not leave this question unanswered:

    answers for this question and similar ones (dealing with the internals of Node-RED) may be found in some postings written by Nick O'Leary for the Node-RED forum

    The following is just a copy from there:

    Everything that follows only applies in the editor. This is no equivalent in the runtime.

    Finding by name

    There's no direct way to find anything by its 'name' - names are optional fields and can be left blank. They are also not unique. Under the covers you should be using the id field to reference other nodes.

    let node = RED.nodes.node(id);
    

    To get any nodes whose name property is set to a particular value:

    let results = [];
    RED.nodes.eachNode(function(node) {
       if (node.name === "HELLO") {
          results.push(node);
       }
    });
    

    (If you dig around the code, you'll see RED.nodes.filterNodes exists - that currently only filters on z and type rather than any other property... but it would be simply enough to update to handle any property... we have never needed it)

    The above works for regular flow nodes. To do the same for Config nodes or Flows, you'd do the same but with eachConfig() and eachWorkspace() respectively.

    Find connected nodes of a given node

    let node = RED.nodes.node(nodeId);
    let allConnectedNodes = RED.nodes.getAllFlowNodes(node)
    

    If you just want the nodes that come 'before' or 'after' this one, you can do:

    let allUpstreamNodes = RED.nodes.getAllUpstreamNodes(node);
    let getAllDownstreamNodes = RED.nodes.getAllDownstreamNodes(node) 
    

    Additional information can be found in another posting, which is also copied below (the first code line illustrates how to access a node by its internal id):

    how to find the name of the tab a given node is placed on

    let node = RED.nodes.node(id)
    
    let tab = RED.nodes.workspace(node.z) || RED.nodes.subflow(node.z);