How to activate the Global Hooks

Hi !
I would like to activate the global hooks as described in Hooks API : Node-RED.

After spending hours and hours searching for any example how to activate them - has anybody any deeper information how to do this ?

Where the "RED.hooks.add(" has to be configured ? Any example or deeper description availiable which I've not found ?

Thanks for your help.

Hooks can be used in custom nodes;

Note: there is also a hooks api in the editor, but it is less mature so not currently documented for general use.

Thanks für your reply. But this one are not the hooks I mean.

I mean the GLOBAL ones - for example "onSend" which will be fired on every Send event in all nodes in all flows (if my understanding of the documentation is right) .

There is an example in the documentation that shows how to use these hooks:

// Example synchronous onSend hook
RED.hooks.add("onSend", (sendEvents) => {
    console.log(`Sending ${sendEvents.length} messages`);
});

Keep in mind that this happens on the server - not in the client.
Shouldn't this be what you're looking for?
If it is not: What do you try to achieve?

Hi ! Thanks for your support. This example describes for me how this hook can be added (and I've tried several of these examples). But what's not clear for me is WHERE to place this "RED.hooks.add" ? I've done it in settings.js (but probably in the wrong section) and so (surely after restarting the server) I'm controlling the Server-Console - but nothing happens.

I would like to log special data of the flows-messages in an a additional log which is not loggable by the standard "node-red" logging options

Probably an additional relevant information : I'm running currently server and client on a WIN10 Environment.

As @bakman2 already stated:

... or plugins.

This sounds like a 'problem' solved several times by the means Node-RED already provides - without the need to hook into the core of the runtime. If you provide some more details (e.g.: Which messages? Where shall those be logged to?), I'm convinced we'll be able to show you a way to go...

The beauty of Node-RED lays in the fact that the platform it is running on - in 9 of 10 cases - doesn't matter at all...

Hi. Thanks for your reply. What I like to do are expand severals options - my description of logging was only a small example.

So what I really need is a short description where GLOBAL hooks has to be registered to use this functionality generally and not to a specific flow in a specific node.

Regards Fred

You need to create a Node-RED plugin that the runtime loads when it starts up.

A plugin is very similar to a normal node module, but it isn't required to provide any nodes for the palette.

For example, node-red-debugger is a plugin that uses the hooks: GitHub - node-red/node-red-debugger: A flow debugger for Node-RED 2.x (written in TypeScript, but the principle is the same).

Hi, Nick !

Thanks for your qualified answer. That‘s exactly what I’m searching for. Surely I need some time to completly analyze your code, but so I’ll be able to do my first tests. With the hooks.

Have a nice time and thanks once again.

Kind Regards

Fred

Hi, Nick,

I’ve done as you proposed and everything oft he examples (also with modifications) runs excellent.

But if I expand one of them for using sqlite3 module I’ll get errors.

First way : using „require(‚sqlite3‘)“ produces „cannot find module sqlite3“ (sqlite has been installed with npm with and without –g option)

Second way : configuring inside settings.js in the functionGlobalContext as

„sqlite3:require('sqlite3'),“ and using „var sqlite3=global.get('sqlite3');“ with the error „TypeError: global.get is not a function“.

I’ve also tried to activate „externalModules“ with „autoInstall: true“ and „modules: {allowInstall: true,} – no change at all.

Any idea where my mistake may be ?

Kind regards

Fred

  1. did it install without error?
  2. what were the exact npm commands you used?
  1. Where was this code written? in a function node? in a custom node/plugin?

Hi,

I think I found a solution last night. It seems that there is an mistake in Node-Red.

The source of my node was placed in an directory on drive „d:......\nodexyz“ The „npm install d:.....“ works fine, but when the node is processed the resolution of the „require(…);“ can not be done and result in the error. After uninstalling the node, moving the subdir of the node to „c:\Users\Name.node-red...“, reinstall it and restart the server was the solution. Right now all modules are loaded and everything works fine. Seems that the sources of nodes can not be located on an other drive !

Thanks for your interest and try to help. Kind Regards.

Have you added sqlite3 to your plugins own package.json file?

Yes and no – I’ve tried both – with same error.

I’ve also checked if sqlite3 has been installed in the node-modules tree.

Everythings seems as is should.

The only way to find the solution was moving the directory of the node tot he same drive/.node-red path.

I think it depends to the relative pathes in the loader. (I don’t remember which .js-file it was – due to dozens of tests)

I believe that you also need to go into that directory and run
npm install
to install the dependencies.

I've did it - but it doesn't help. Thx.

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