adammo
adammo

Reputation: 211

Injecting many payloads at once (node-red)

I have a problem. I'm new to node red, I want to inject many payloads with different topics at once. I wanted to do it with function like in first node. It's function looks like so:

msg.topic="ns=2;s=Target01.Nazwa.Nazwa[0];datatype=String"
msg.payload=global.get("nazwa")
return msg
msg.topic="ns=2;s=Target01.Nazwa.Nazwa[1];datatype=String"
msg.payload=global.get("nazwa2")
return msg
...
msg.topic="ns=2;s=Target01.Nazwa.Nazwa[9];datatype=String"
msg.payload=global.get("nazwa9")
return msg

enter image description here

However it doesn't work. The 2nd node is working but in total I would have like 150+ blocks connected to OPC UA Client block. So my question is: does anyone know if there's a way to inject multiple payloads with different topics, favorabily with function, instead of doing it one by one with inject blocks?

Upvotes: 0

Views: 2356

Answers (1)

knolleary
knolleary

Reputation: 10117

The documentation explains how to send multiple messages from a status node.

With the code you have currently, as soon as it reaches the first return statement, the Function node stops processing any further so only one message is sent.

To send multiple messages from a Function node you have two options.

  1. return an array of message objects to send.
  2. call node.send(msg); for each message you want to send.

For example:

return [
 [
   { topic: "ns=2;s=Target01.Nazwa.Nazwa[0];datatype=String", payload: global.get("nazwa")},
   { topic: "ns=2;s=Target01.Nazwa.Nazwa[1];datatype=String", payload: global.get("nazwa2")},
   { topic: "ns=2;s=Target01.Nazwa.Nazwa[9];datatype=String", payload: global.get("nazwa9")}
  ]
]

Upvotes: 4

Related Questions