When is registerType used/called/accessed?

Hi There,

I was looking through the code base of knxEasy node - which hasn't been touched in five years, so I don't know whether it's being maintained.

I was looking at the server-side code for the knxEasy-in node:

module.exports = function (RED) {
    function knxEasyIn(config) {
        RED.nodes.createNode(this, config)
        var node = this

        ... initialiisation code remove ....

        if (node.server) {
            if (node.topic) {
                node.server.register("in", node)
            }
        }
    }
    RED.nodes.registerType("knxEasy-in", knxEasyIn)
}

What interested me was the node.server.register("in", node) which registers the nodes configuration with a central listener for knx events.

That configuration is provided in the editor panel and is basically the group address and datapoint type:

Here my confusion: my understanding is that if this configuration is modified in the editor, then registerType isn't called on the server, i.e. the register block above isn't executed again to update the central knx listener via the node.server.register function.

So when does the code registered with the RED.nodes.registerType function get called when:

  1. a node is added to the workspace? - Yes I would say.
  2. a flow is deployed or rather a node is updated by a deploy? - Yes or no - I'm not sure?
  3. when I update the parameters of the node in the editor? No I would say.
  4. When Node-RED restarts/starts? - Yes I would say.

The point of this is that if I update the knxEasy-in node in the editor, do those changes even take affect on the server? Or better said, when do they take affect...

I did have a quick skim of the docu and that would seem to imply the code is only called once the node is dragged onto the workspace - which also implies on start or restarts of flows.

Perhaps I'm imagining all this and the knxEasy-in node works fine (I can't test it since I don't have a knx product) :wink:

RED.nodes.registerType

Is called for every installed/identified node at runtime startup
its why the exported function needs to have at.

module.exports = function(RED) {

    ... Magic

    function CallMeWhenNeeded(...){
       ... More Magic
    }

    RED.nodes.registerType("CallMeWhenNeeded", CallMeWhenNeeded)
}

The function passed into it, is executed when the node its self is started.
(started also meaning edited/re-deployed )

This registration run is identified by this log entry

2 Dec 19:47:41 - [info] Loading palette nodes

Example - I create some HTTP Admin endpoints in this anonymous function - so A node doesnt need to be in the flow, to have it available

module.exports = function(RED) {

    ... Magic

    function CallMeWhenNeeded(...){
       ... More Magic
    }

    RED.nodes.registerType("CallMeWhenNeeded", CallMeWhenNeeded)
    RED.httpAdmin.get('/magic-url',...)
}

Ah ok, so it is called when the node is updated in the editor and the flow is (re-)deployed since the node changed. That bit of logic was missing in my thinking.

But this function:

module.exports = function(RED) {
 ...
}

is called once at Node-RED start up so that the node type is also only registered once. Hence also your http admin endpoint is also only created once.

Cheers for the explanation :+1:

P.S.

hehe, that should be CalledWhenEverTheNodeChangesInTheWorkspace

1 Like

If you look at this repo:

You will see an alternative layout for node runtimes that may help make more sense of when things are called and how data and the RED objects are referenced.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.