If you need to send message when your node is initialized, then do it in your constructor with process.nextTick or some other delay mechanism. this works. I just tried it with my custom node.
// send yourself a message
process.nextTick(() => {
this.emit('input', { payload: 'test input'});
});
// send the next node a message
process.nextTick(() => {
this.send({ payload: 'test input 2'});
});
Oops! Misread, sorry. Thanks for the clarification and of course you are correct.
Just did a quick test and added this to my nodeInstance function after RED.nodes.createNode(this, config) so that the this object was correctly initialised.
I get a log output for each instance of uibuilder loaded into my flow of course but still nothing gets sent so obviously still too early. It also generates a maxListeners warning though that is probably a red herring.
It works as expected (a message sent for each instance of the node in my flows).
You can't put it any earlier in the process though as you don't have a send function except on the initialised node instance - as far as I am aware anyway.
So you should add a flag if you only want a single output - then the first instance of the node would send the msg and the others would be blocked.
It ensures there's a _msgid and then emits the input event.
Either approach is entirely valid.
@kevinGodell the problem using nextTick is that it assumes all nodes start synchronously. If anything has to do any async work then it could get scheduled after nextTick.
Whilst the setTimeout approach isnt the most elegant, it is pretty tried and tested with the Inject node.