Search code examples
javascriptnode.jsjsonnode-red

Node-RED Sending multiple msg only returning first msg


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.

Flow

Processing1 code:

var msg1 = {};
var msg2 = {};

msg1.topic = "fooTopic1";
msg2.topic = "barTopic1";

msg1.payload = "fooLoad1";
msg2.payload = "barLoad1";

return [msg1,msg2];

Result:

Result Processing1

Processing2 code:

var msg1 = {};
var msg2 = {};

msg1.topic = "fooTopic2";
msg2.topic = "barTopic2";

msg1.payload = "fooLoad2";
msg2.payload = "barLoad2";

return msg1,msg2;

Result Processing2

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


Solution

  • 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.