# Messages distribution

**URL:** https://discourse.nodered.org/t/messages-distribution/47524
**Category:** General
**Created:** [23 June 2021 11:00 UTC](https://discourse.nodered.org/t/messages-distribution/47524 "2021-06-23T11:00:08Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)
#### Post date: [23 June 2021 11:00 UTC](https://discourse.nodered.org/t/messages-distribution/47524/1 "2021-06-23T11:00:08Z")

</div>

I have "text input" node and I need that the 1st message appear in Output 1, then 2nd message in output 2 and so on. And after 4 messages, the 5th message will again in output 1.  
Below picture shows how the sketch will be look like, but what I need to write in "function node".  
 ![info](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d48a5aabdd64055709b6267529097bd875c7433.jpeg)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 June 2021 11:28 UTC](https://discourse.nodered.org/t/messages-distribution/47524/2 "2021-06-23T11:28:55Z")

</div>

Have a read through the node red docs page Writing Functions. That will show you how to have multiple outputs on a function node and also how to use the node context to remember a value from one message to the next, so that you can remember which output to send the next message to.

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [23 June 2021 13:14 UTC](https://discourse.nodered.org/t/messages-distribution/47524/3 "2021-06-23T13:14:51Z")

</div>

As Colin said - have a read through the docs page, well worth the time and effort.

To get you started here's a piece of code to direct a message to an output depending on Topic value.

```auto
if (msg.topic == "monitor_A") {
    return [msg, null, null, null, null];
}
else if (msg.topic == "monitor_B") {
    return [null, msg, null, null, null];
}
else if (msg.topic == "monitor_C") {
    return [null, null, msg, null, null];
}
else if (msg.topic == "monitor_D") {
    return [null, null, null, msg, null];
}
else if (msg.topic == "main_display") {
    return [null, null, null, null, msg];
}
else return null;

```

![Screen Shot 06-23-21 at 02.13 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/759f94172c355cab9b8de7bfbb25d01eb920c1e3.jpeg)

---

<div class="post-metadata">

### Author: ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)
#### Post date: [25 June 2021 10:57 UTC](https://discourse.nodered.org/t/messages-distribution/47524/4 "2021-06-25T10:57:20Z")

</div>

Thanks for your reply, actually, msg.payload is count based instead of topic based

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 June 2021 13:05 UTC](https://discourse.nodered.org/t/messages-distribution/47524/5 "2021-06-25T13:05:48Z")

</div>

Why do you want to do that, it seems an odd requirement.

---

<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: [25 June 2021 14:34 UTC](https://discourse.nodered.org/t/messages-distribution/47524/6 "2021-06-25T14:34:48Z")

</div>

If count base then try this

```auto
[{"id":"e28d32af.7b1b6","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":1440,"wires":[["d796969f.c0815"]]},{"id":"d796969f.c0815","type":"function","z":"c74669a0.6a34f8","name":"","func":"let count = context.get(\"count\") || 0;\nlet msgArray =[null,null,null,null];\nmsgArray[count] = msg ;\n\ncontext.set(\"count\", (count >= 3 ? 0 : count+1));\nreturn msgArray;","outputs":4,"noerr":0,"initialize":"","finalize":"","x":310,"y":1440,"wires":[["2ab3b436.e9ebf4"],["a2ce40b1.b93808"],["a286052a.56158"],["6ac62665.f01dc"]]},{"id":"2ab3b436.e9ebf4","type":"debug","z":"c74669a0.6a34f8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":500,"y":1400,"wires":[]},{"id":"a2ce40b1.b93808","type":"debug","z":"c74669a0.6a34f8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":1440,"wires":[]},{"id":"a286052a.56158","type":"debug","z":"c74669a0.6a34f8","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":1480,"wires":[]},{"id":"6ac62665.f01dc","type":"debug","z":"c74669a0.6a34f8","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":1520,"wires":[]}]

```

```auto
let count = context.get("count") || 0;
let msgArray =[null,null,null,null];
msgArray[count] = msg ;
context.set("count", (count >= 3 ? 0 : count+1));
return msgArray;

```

---

<div class="post-metadata">

### Author: ![Swen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/swen/32/43902_2.png) [@Swen](https://discourse.nodered.org/u/Swen)
#### Post date: [25 June 2021 15:48 UTC](https://discourse.nodered.org/t/messages-distribution/47524/7 "2021-06-25T15:48:34Z")

</div>

moin!  
There are several packages available at the palette.

keyword = content based router or router in common

There will be light...

cu  
Swen

---

<div class="post-metadata">

### Author: ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)
#### Post date: [25 June 2021 16:38 UTC](https://discourse.nodered.org/t/messages-distribution/47524/8 "2021-06-25T16:38:51Z")

</div>

Actually, I have input node at Nod-red dashboard to enter the values and sending these values to S7 plc and I need the values in series (rows)... Means 1st payload will show 1output (store in 1st row),, 2nd payload will show in 2nd output (store in 2nd row) and so on. And In dashboard, I don`t want to show individual nodes for individual inputs.

---

<div class="post-metadata">

### Author: ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)
#### Post date: [25 June 2021 16:40 UTC](https://discourse.nodered.org/t/messages-distribution/47524/9 "2021-06-25T16:40:40Z")

</div>

Thanks, It is working

---

<div class="post-metadata">

### Author: ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)
#### Post date: [25 June 2021 16:41 UTC](https://discourse.nodered.org/t/messages-distribution/47524/10 "2021-06-25T16:41:43Z")

</div>

thanks for your suggestion @Swen

---

<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 2021 16:41 UTC](https://discourse.nodered.org/t/messages-distribution/47524/11 "2021-07-09T16:41:46Z")

</div>

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