Responding to a node's 'send' event

Hi all,

I am currently using a common library in which I keep common functionality for a large set of node types. I obviously do this to avoid repeated code and to promote code maintainability.

As it stands, all my node types call a registration function within the common library, using the node's this object as a parameter. As such, I am able to refer to any of these nodes from the common library, for example if I wanted to respond to an input event of a given node I could do...

nodes[<ID OF NODE>].on('input', () = > {
     console.log(`I have detected the input from the common library`)
})

where nodes is just an object containing all the current nodes, keyed by the node id.

What I really need to do, is from common library, respond to when any of the registered nodes perform a send. In an ideal world the code would look something like this...

nodes[<ID OF NODE>].on('send', () = > {
     console.log(`I have detected the node's send event from my common library`)
})

However this is not possible as node-RED does not emit an event called 'send' when a given node sends (correct me if I am wrong here, I could not see one or an equivalent when digging through the node-RED source).

If there is a way around this then I would be open to suggestions. I am reluctant to add any new code into the node types themselves as there are a very large number of node types in which I would have to edit every single one. It's also messy as this functionality is common and therefore should reside within the common library. As such, if possible, I am only interested in solutions involving changing the common library only.

Thanks.

Each node instance has its own send function, I think that you would have to change the nodes to use your own function instead and from there use the node's actual function along with any other processing you wanted. You could, for example, use that send wrapper to create an event.

You should also note that a node instance's on input function is also passed a send parameter that you may also want to wrap.

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