Is it possible to create two separate messages in parallel?

Good evening everyone,

I have a function node that creates a msg.payload and msg.topic.
I would like to output that msg to output 1 of the function node.

So far no problem.
Within that same function I would like to create a second msg.payload and msg.topic which should be output to output 2.

So something like

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg.topic = "topic 1" + [i]
        msg.payload = "payload 1" + [i]
    node.send([msg, null])
}}

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg.topic = "topic of message 2" + [i]
        msg.payload = "payload 2"  + [i] 
    node.send([null, msg])
}}

Is that possible?

I amtrying to avoid creating the first message, outputting it via node.send([msg, null]) and then creating the second and outputting it to out 2 due to the asyncronous behaviour of node-red (since in both cases I will be creating msg.payload and msg.topic and I do not want them to mix and match).

Thank you everyone :slight_smile:
Alex

Hi @AleXSR700

I'm not sure I have this right, but is something like this?

you don't have to re-use the same msg object - and shouldn't really

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        
        const Message = {
            topic:`topic 1 ${[i]}`,
            payload: `payload 1 ${[i}`
        }
        
        node.send([Message, undefined])
    }
}

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {

        const Message = {
            topic:`topic 2 ${[i]}`,
            payload: `payload 2 ${[i]}`
        }
        
        node.send([undefined,Message])
    }
}

Ah, so maybe I could simply use msg1.payload and msg2.payload?

So msg is not a defined "property" of a flow?

msg is the name of the "package" so to speak

So if you do.

node.send({someProperty:"Hello"})

The next Node along will obtain it with it

msg.someProperty

you can manipulate the "package" during its transport through other Nodes, or create a new package, by simply defining a new object (you don't have to call it msg) but Node RED will send your package wrapped in a property called msg for the next node

Ah, but then I cannot just do

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg1.topic = "topic 1" + [i]
        msg1.payload = "payload 1" + [i]
        msg2.topic = "topic of message 2" + [i]
        msg2.payload = "payload 2"  + [i] 
    node.send([msg1, msg2])
}}

?

Almost

// define the packages first
const msg1 = {}
const msg2 = {}

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {
        msg1.topic = "topic 1" + [I]
        msg1.payload = "payload 1" + [I]
        msg2.topic = "topic of message 2" + [I]
        msg2.payload = "payload 2"  + [I] 
    node.send([msg1, msg2])
}}

unless I am not following :sweat_smile:

No no, you are. That works perfectly, thank you.

So msg does not need to be defined but since Node-Red does not know additional messages (msg[i]) I have to use contexts rather than the built-in msg.

Not strictly.

but each node downstream will still receive an msg object - but each being not related (as you are creating new objects for each output)
And in that, any properties you added (payload, topic. etc etc)

msg.payload
msg.topic

if you don't create a new object (package) - all you are doing is manipulating the current msg flowing through

I called it a package - but was stuck for words :smile:

1 Like

@AleXSR700

Just realised a slight flaw.

move the object creation inside your loop.
else the next iteration will just modify the previous msg1, and msg2 object that was sent on the previous iteration.

if you manipulate something that was already sent, the nodes dealing with the previous iteration will see the modifications - you probably don't want that, so create a new object(s) for each loop

for (let i = 0; i < devices.length; i++) {
    for (let j = 0; j < entities.length; j++) {

        const msg1 = {}
        const msg2 = {}

        msg1.topic = "topic 1" + [i]
        msg1.payload = "payload 1" + [i]
        msg2.topic = "topic of message 2" + [i]
        msg2.payload = "payload 2" + [i]

        node.send([msg1, msg2])
    }
}

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