Node-Red problem with messages in node.send(msg)

Hey,

I have problems with the call: node.send(msg).

When I call node.send(msg) within node.on('input', function(msg){}), it works, but this call within function Test(n) {} does not work always (sometimes, if the flow already exists!, but never after the whole flow was imported).
I read docs regarding node.send(msg), e.g. How node.send() works, but I cannot fix this.
Is this call synchronous (How node.send() works)?
Should I call node.send(msg) within an asynchronous context?

Please help to get the right direction.

Many thanks.

If you call node.send in your Node's constructor then you are calling it when the runtime is still starting up the flows. There is no guarantee that whatever your node is wired to exists yet.

Assuming you want your node to send a message each time your flows start (or are restarted due to a deploy etc) then you should wrap it in a setTimeout call with a short delay so the runtime can finish starting the flows before your node starts sending.

Hey Nick,
thank you for the quick response. :slight_smile: