Reputation: 1205
I'm trying to send multiple messages using NodeRed
.
I've tried two options: Trying to send using one output, and trying to send using two outputs.
Processing1 code:
var msg1 = {};
var msg2 = {};
msg1.topic = "fooTopic1";
msg2.topic = "barTopic1";
msg1.payload = "fooLoad1";
msg2.payload = "barLoad1";
return [msg1,msg2];
Result:
Processing2 code:
var msg1 = {};
var msg2 = {};
msg1.topic = "fooTopic2";
msg2.topic = "barTopic2";
msg1.payload = "fooLoad2";
msg2.payload = "barLoad2";
return msg1,msg2;
I was expecting 2 seperate messages, but debug only outputs one. What am I doing wrong?
Flow for those who want to test for themselves:
[
{
"id": "5824c01b.a28ab",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": ""
},
{
"id": "4902c067.f3c27",
"type": "inject",
"z": "5824c01b.a28ab",
"name": "Input",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 150,
"y": 220,
"wires": [
[
"6a361af6.b5edf4"
]
]
},
{
"id": "6a361af6.b5edf4",
"type": "function",
"z": "5824c01b.a28ab",
"name": "Processing2",
"func": "var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopic2\";\nmsg2.topic = \"barTopic2\";\n\nmsg1.payload = \"fooLoad2\";\nmsg2.payload = \"barLoad2\";\n\nreturn msg1,msg2;",
"outputs": 2,
"noerr": 0,
"x": 350,
"y": 220,
"wires": [
[
"727ebca3.2270a4"
],
[
"727ebca3.2270a4"
]
]
},
{
"id": "727ebca3.2270a4",
"type": "debug",
"z": "5824c01b.a28ab",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 530,
"y": 220,
"wires": []
},
{
"id": "6b1d928e.2752bc",
"type": "inject",
"z": "5824c01b.a28ab",
"name": "Input",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 150,
"y": 160,
"wires": [
[
"cc02ad73.9febe"
]
]
},
{
"id": "cc02ad73.9febe",
"type": "function",
"z": "5824c01b.a28ab",
"name": "Processing1",
"func": "var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopic1\";\nmsg2.topic = \"barTopic1\";\n\nmsg1.payload = \"fooLoad1\";\nmsg2.payload = \"barLoad1\";\n\nreturn [msg1,msg2];",
"outputs": 1,
"noerr": 0,
"x": 350,
"y": 160,
"wires": [
[
"a8e66620.492e48"
]
]
},
{
"id": "a8e66620.492e48",
"type": "debug",
"z": "5824c01b.a28ab",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 530,
"y": 160,
"wires": []
}
]
Node-RED v.0.20.7
Upvotes: 4
Views: 10271
Reputation: 857
It depends on your needs. Do you want to send multiple messages to different destinations or to the same destination?
You need to set multiple outputs (e.g. two outputs), and use return [msg1,msg2];
. Full code:
var msg1 = {};
var msg2 = {};
msg1.topic = "fooTopicA";
msg2.topic = "barTopicA";
msg1.payload = "fooLoadA";
msg2.payload = "barLoadA";
return [msg1,msg2];
You don't need to set multiple outputs, you need just one. In your function node use return [[msg1,msg2]];
. Full code:
var msg1 = {};
var msg2 = {};
msg1.topic = "fooTopicB";
msg2.topic = "barTopicB";
msg1.payload = "fooLoadB";
msg2.payload = "barLoadB";
return [[msg1,msg2]];
Full flow for testing:
[{"id":"5da1a303.2f8bbc","type":"tab","label":"test","disabled":false,"info":""},{"id":"2b48081b.61be58","type":"inject","z":"5da1a303.2f8bbc","name":"Input","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":300,"wires":[["c7169d64.37036"]]},{"id":"c7169d64.37036","type":"function","z":"5da1a303.2f8bbc","name":"Processing B","func":"var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopicB\";\nmsg2.topic = \"barTopicB\";\n\nmsg1.payload = \"fooLoadB\";\nmsg2.payload = \"barLoadB\";\n\nreturn [[msg1,msg2]];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":300,"wires":[["6b11270f.3477a8"]]},{"id":"6b11270f.3477a8","type":"debug","z":"5da1a303.2f8bbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":300,"wires":[]},{"id":"6cd03253.f1e09c","type":"inject","z":"5da1a303.2f8bbc","name":"Input","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":180,"wires":[["d9b50766.e9c9a8"]]},{"id":"d9b50766.e9c9a8","type":"function","z":"5da1a303.2f8bbc","name":"Processing A","func":"var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopicA\";\nmsg2.topic = \"barTopicA\";\n\nmsg1.payload = \"fooLoadA\";\nmsg2.payload = \"barLoadA\";\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":370,"y":180,"wires":[["abde843d.cbf888"],["e99faf37.fe8b3"]]},{"id":"abde843d.cbf888","type":"debug","z":"5da1a303.2f8bbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":160,"wires":[]},{"id":"e99faf37.fe8b3","type":"debug","z":"5da1a303.2f8bbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":220,"wires":[]}]
Upvotes: 3
Reputation: 59668
This is because function nodes can have multiple output ports.
To send multiple messages out of a single output port you need to double wrap the messages in square brackets.
return [[msg1,msg2]]
This is because a single array tells Node-RED to send msg1
from the first port and msg2
from the second port.
Using a 2D array says send msg1
& msg2
from port 1.
Upvotes: 4