# Sending multiple payloads to different hosts at once

**URL:** <https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331>\
**Category:** General\
**Tags:** http-request, function-node\
**Created:** [24 June 2022 17:14 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331 "2022-06-24T17:14:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielEcheverri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danielecheverri/32/62150_2.png) [@DanielEcheverri](https://discourse.nodered.org/u/DanielEcheverri)\
**Post date:** [24 June 2022 17:14 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/1 "2022-06-24T17:14:53Z")

</div>

Hi! I have two input text fields where the user enters two variables: _replySource_ and _replyCode_. The _replySource_ variable acts as the ID of a single device in the network. For instance, in the code below, _replySource_ "20" sends the payload _replyCode_ AND _replySource_ upon the click of a button to device 20 on that specific IP address. The payload ends up being _msg.url_ for a POST request node. So far this part is working (I am not including here the rest of the flow, as the flow doesn't seem to be the problem).

However, there is a case in which I need to send the same payload in _replyCode_ to all three devices in the network at the same time when the value in _replySource_ is equal to 00 along with their original ID as _replySource_ (that's why I use flow. set). I thought about the function below but does not seem to work. I am very new to NodeRed and Java, so I appreciate any guidance in this regard.

This is how the flow is organized

 ![Screen Shot 2022-06-24 at 19.13.43 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/e/0e40d0fbaf71861230b9a67e51451926c406c2d9.png)

```auto
var replySourceVal = flow.get('replySource');

//SEND TO ONE DEVICE
if (replySourceVal == "20") { //Red
msg.payload = "192.168.0.20/reply";
}

//SEND TO ALL DEVICES
if (replySourceVal == "00") { 
for (var i = 0; i < 3; i++) {
        if (i==1){
          flow.set('replySource', "20");
          msg.payload = "192.168.0.20/reply";
        }
        if (i==2){
           flow.set('replySource', "30");
           msg.payload = "192.168.0.30/reply";
       }
       if (i==3){
           flow.set('replySource', "40");
           msg.payload = "192.168.0.40/reply";
       }
    }
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [24 June 2022 17:25 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/2 "2022-06-24T17:25:57Z")

</div>

A very quick look at this, you can use `node.send()`  
`node.send` does exactly that, sends without breaking out of the function node.

```javascript
//SEND TO ALL DEVICES
if (replySourceVal == "00") { 
for (var i = 0; i < 3; i++) {
        if (i==1){
          flow.set('replySource', "20");
          msg.payload = "192.168.0.20/reply";
          node.send(msg);
        }
        if (i==2){
          flow.set('replySource', "30");
          msg.payload = "192.168.0.30/reply";
          node.send(msg);
       }
       if (i==3){
          flow.set('replySource', "40");
          msg.payload = "192.168.0.40/reply";
          node.send(msg);
       }
    }
}
//return msg - you don't need this (unless you do :-)

```

---

<div class="post-metadata">

**Author:** ![DanielEcheverri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danielecheverri/32/62150_2.png) [@DanielEcheverri](https://discourse.nodered.org/u/DanielEcheverri)\
**Post date:** [24 June 2022 17:43 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/3 "2022-06-24T17:43:24Z")

</div>

Hi Marcus, thank you so much for the suggestion. Adding `node.send();` doesn't seem to work. For some reason, the payload is not sent, but instead, remains to be the same payload from the previous successful delivery to a single device (hope I am being clear). Is as if the function does not enter into the `for` loop. I tried using `node.warn` to do some debugging, but oddly enough it only works once.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [24 June 2022 17:47 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/4 "2022-06-24T17:47:30Z")

</div>

Mmm.

I changed the loop slightly - and got the expected result.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3da32794ac0e4daf86e413b69e27566f7e4fa755.png)

```auto
for (var i = 0; i < 3; i++) {
    if (i == 0) {
        flow.set('replySource', "20");
        msg.payload = "192.168.0.20/reply";
        node.send(msg);
    }
    if (i == 1) {
        flow.set('replySource', "30");
        msg.payload = "192.168.0.30/reply";
        node.send(msg);
    }
    if (i == 2) {
        flow.set('replySource', "40");
        msg.payload = "192.168.0.40/reply";
        node.send(msg);
    }
}

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [24 June 2022 17:52 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/5 "2022-06-24T17:52:03Z")

</div>

Oh! hang on - you may need to include `replySource` or `replyCode` in your msg - what ever should be sent  
`node.send` is `async` I believe

maybe attach it to `msg.replySource` or `msg.replyCode` - then use that in the next node along, as the flow var maybe changed at the time of the other sends (given its asynchronous nature)

---

<div class="post-metadata">

**Author:** ![DanielEcheverri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danielecheverri/32/62150_2.png) [@DanielEcheverri](https://discourse.nodered.org/u/DanielEcheverri)\
**Post date:** [24 June 2022 18:28 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/6 "2022-06-24T18:28:39Z")

</div>

Good suggestion. Indeed the changes in the code seem to work. However, I think the issue with the loop is somewhere in the button, as once it successfully delivers a message, then it stops sending the subsequent ones. For instance, if the host in `192.168.0.20/reply` is down, then it moves to the next host which will deliver the right payload to the right device. Thank you @marcus-j-davies anyway for the time 🙂

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 June 2022 19:06 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/7 "2022-06-24T19:06:16Z")

</div>

why not make flow replySource an array of objects, then use a split node to separate the http requests.  
e.g.

```auto
[{"id":"b3864866e9cd631c","type":"ui_text_input","z":"bf9e1e33.030598","name":"replySource","label":"Source","tooltip":"","group":"dfc9abd922154e9a","order":3,"width":0,"height":0,"passthru":true,"mode":"number","delay":"300","topic":"source","sendOnBlur":true,"className":"","topicType":"str","x":160,"y":2940,"wires":[["4f7ca82a.bab14"]]},{"id":"4f7ca82a.bab14","type":"join","z":"bf9e1e33.030598","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":310,"y":2980,"wires":[["008fea195280df1e"]]},{"id":"36d4bdfbad777b18","type":"ui_text_input","z":"bf9e1e33.030598","name":"replyCode","label":"Code","tooltip":"","group":"dfc9abd922154e9a","order":2,"width":0,"height":0,"passthru":true,"mode":"number","delay":"300","topic":"code","sendOnBlur":true,"className":"","topicType":"str","x":150,"y":3000,"wires":[["4f7ca82a.bab14"]]},{"id":"008fea195280df1e","type":"function","z":"bf9e1e33.030598","name":"","func":"if (msg.payload.source == \"00\") { \n msg.payload = [\n {url: \"192.168.0.20/reply\", source:\"20\", code: msg.payload.code},\n {url: \"192.168.0.30/reply\", source: \"30\", code: msg.payload.code},\n {url: \"192.168.0.40/reply\", source: \"40\", code: msg.payload.code}\n]\n}else{\n msg.payload = [{\n url: `192.168.0.${msg.payload.source}/reply`, \n source: msg.payload.source,\n code: msg.payload.code\n }];\n}\nflow.set(\"replySource\", msg.payload);\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":2980,"wires":[[]]},{"id":"fac0982110da23a1","type":"ui_button","z":"bf9e1e33.030598","name":"","group":"dfc9abd922154e9a","order":2,"width":0,"height":0,"passthru":false,"label":"Send","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"replySource","payloadType":"flow","topic":"topic","topicType":"msg","x":150,"y":3100,"wires":[["2cebd59f.44b05a"]]},{"id":"2cebd59f.44b05a","type":"switch","z":"bf9e1e33.030598","name":"ceck for one valid payload","property":"$count(payload[0].*)","propertyType":"jsonata","rules":[{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":360,"y":3100,"wires":[["94bc3175.605be"]]},{"id":"94bc3175.605be","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":390,"y":3160,"wires":[["dbbd9ac25d0245b7"]]},{"id":"dbbd9ac25d0245b7","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload.url","pt":"msg","to":"url","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":3160,"wires":[["8a53079e89f84ec9"]]},{"id":"8a53079e89f84ec9","type":"debug","z":"bf9e1e33.030598","name":"http","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":3160,"wires":[]},{"id":"dfc9abd922154e9a","type":"ui_group","name":"InputCommand","tab":"e562a4a.39a9e58","order":4,"disp":true,"width":"6","collapse":false,"className":""},{"id":"e562a4a.39a9e58","type":"ui_tab","name":"Non-myth of the Noble Red's Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

Needs a bit more validation on accepted sources.

---

<div class="post-metadata">

**Author:** ![DanielEcheverri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danielecheverri/32/62150_2.png) [@DanielEcheverri](https://discourse.nodered.org/u/DanielEcheverri)\
**Post date:** [25 June 2022 07:50 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/8 "2022-06-25T07:50:27Z")

</div>

Thank you @E1cid. Indeed the flow as you suggested works perfectly. Just in case anyone is interested, I did a couple of tweaks but you can get the flow here or directly from E1cid's original reply:

```auto
[{"id":"2e55dac334a66988","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"749c787253047e22","type":"ui_text_input","z":"2e55dac334a66988","name":"replySource","label":"Source","tooltip":"","group":"aa948ca87c80c30e","order":3,"width":0,"height":0,"passthru":true,"mode":"number","delay":"0","topic":"source","sendOnBlur":true,"className":"","topicType":"str","x":150,"y":100,"wires":[["a82420ade7ebdddc","fc2794d0878bd9cc"]]},{"id":"a82420ade7ebdddc","type":"join","z":"2e55dac334a66988","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":320,"y":140,"wires":[["5bc5e4e4b6a9b2ae","36e6554f953b6115"]]},{"id":"d516b7b6b5cd4d12","type":"ui_text_input","z":"2e55dac334a66988","name":"replyCode","label":"Code","tooltip":"","group":"aa948ca87c80c30e","order":2,"width":0,"height":0,"passthru":true,"mode":"number","delay":"0","topic":"code","sendOnBlur":true,"className":"","topicType":"str","x":150,"y":160,"wires":[["a82420ade7ebdddc","a603847c0eb40766"]]},{"id":"5bc5e4e4b6a9b2ae","type":"function","z":"2e55dac334a66988","name":"","func":"if (msg.payload.source == \"00\") { \n msg.payload = [\n {url: \"192.168.0.20/reply\", source:20, code: msg.payload.code},\n {url: \"192.168.0.75/reply\", source: 30, code: msg.payload.code},\n {url: \"192.168.0.38/reply\", source: 40, code: msg.payload.code}\n]\n}else{\n msg.payload = [{\n url: `192.168.0.${msg.payload.source}/reply`, \n source: msg.payload.source,\n code: msg.payload.code\n }];\n}\nflow.set(\"replySource\", msg.payload);\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":140,"wires":[["dbef62bd002b01b7"]]},{"id":"f9216aaa8d826537","type":"ui_button","z":"2e55dac334a66988","name":"","group":"aa948ca87c80c30e","order":2,"width":0,"height":0,"passthru":false,"label":"Send","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"replySource","payloadType":"flow","topic":"topic","topicType":"msg","x":160,"y":260,"wires":[["0c8b81b7014b0d22"]]},{"id":"0c8b81b7014b0d22","type":"switch","z":"2e55dac334a66988","name":"ceck for one valid payload","property":"$count(payload[0].*)","propertyType":"jsonata","rules":[{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":260,"wires":[["e673b50d731f5ddc"]]},{"id":"e673b50d731f5ddc","type":"split","z":"2e55dac334a66988","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":400,"y":320,"wires":[["60a454b9bcf4151f"]]},{"id":"60a454b9bcf4151f","type":"change","z":"2e55dac334a66988","name":"","rules":[{"t":"move","p":"payload.url","pt":"msg","to":"url","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":320,"wires":[["15b1db897bd10129","0616499c7de59f2a"]]},{"id":"0616499c7de59f2a","type":"debug","z":"2e55dac334a66988","name":"http","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":360,"wires":[]},{"id":"36e6554f953b6115","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":80,"wires":[]},{"id":"dbef62bd002b01b7","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":140,"wires":[]},{"id":"a603847c0eb40766","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":170,"y":220,"wires":[]},{"id":"fc2794d0878bd9cc","type":"debug","z":"2e55dac334a66988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":130,"y":40,"wires":[]},{"id":"15b1db897bd10129","type":"http request","z":"2e55dac334a66988","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":810,"y":320,"wires":[[]]},{"id":"aa948ca87c80c30e","type":"ui_group","name":"Dev2","tab":"e562a4a.39a9e58","order":5,"disp":true,"width":"6","collapse":false,"className":""},{"id":"e562a4a.39a9e58","type":"ui_tab","name":"Dashboard","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 July 2022 07:51 UTC](https://discourse.nodered.org/t/sending-multiple-payloads-to-different-hosts-at-once/64331/9 "2022-07-09T07:51:06Z")

</div>

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