Multiple send(msg) in custom nodes

Sorry if a repost (please point me in case this was already discusses)

I am developing a custom node which needs to do multiple sends from within its input handler. I see a difference between custom & function nodes:

In a function node, if I use node.send(msg) multiple times, it will send separate msg instances. For example:
msg.payload = "AAA"
msg.payload = "BBB"
=> will send the payloads AAA, BBB as intended.

However in a custom node, the runtime seems to send twice the same msg instance (in its latest state). For example:

node.on('input', function(msg, send, done) {
msg.payload = "AAA"
msg.payload = "BBB"
=> will send BBB, BBB.

Is this is because the runtime defers the actual sending, hence 1st msg is impacted by the modification for the 2nd send?

I can work around this by creating new msg instances (let msg2 = {payload:"BBB"} etc.), but just wanted to know if I should be doing something else.


in a roundabout way, yes.

read here: Cloning messages in a flow : Node-RED - section Cloning by default

Cloning by default

With Node-RED 1.0, when a Function node calls node.send(), it will now clone every single message, including the first. This will ensure code like that above will continue to work.

Have a read of that page.

NOTE: Typically, the user will expect the original msg properties in the output of your node - it is up to you as a node designer to decide what cloning is necessary.

1 Like

Thanks Steve!

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