# Split messages arriving out of order

**URL:** <https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661>\
**Category:** General\
**Created:** [4 December 2022 10:57 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661 "2022-12-04T10:57:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 10:57 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/1 "2022-12-04T10:57:03Z")

</div>

As part of my Advent of Code efforts, I'm having to be creative in splitting messages

In Day 3, I need to split a message into two equal parts so I'm using a switch to add msg.complete after half the number of messages have passed

However, I'm finding that I have to stick a superfluous change node in as well otherwise the messages arrive out of order.

Can someone explain why this is happening without the extra change node - is it just timiing? And if so - is there a better way of ensuring correct order of messages arriving at the join?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/70dbdff3f08705c66d1b9e181328b1b928fbc1e7.png)

```auto
[{"id":"273b95be5f0a745f","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"b41a0bea9b144b3a","type":"junction","z":"273b95be5f0a745f","x":840,"y":180,"wires":[["86e060fbfa932ea5","0dc5f6c75011bcbf","7901ee4977a08814"]]},{"id":"1dd4fa914ea28353","type":"split","z":"273b95be5f0a745f","name":"","splt":" 1","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":270,"y":160,"wires":[["8cfe1f4b84945ba0"]]},{"id":"8cfe1f4b84945ba0","type":"switch","z":"273b95be5f0a745f","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"index1","vt":"msg"},{"t":"eq","v":"index2","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":410,"y":160,"wires":[["8194868092e2cd12"],["8194868092e2cd12"],["dfec8efbf4635efc"]]},{"id":"8194868092e2cd12","type":"change","z":"273b95be5f0a745f","name":"","rules":[{"t":"set","p":"complete","pt":"msg","to":"true","tot":"bool","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":140,"wires":[["b41a0bea9b144b3a"]]},{"id":"86e060fbfa932ea5","type":"debug","z":"273b95be5f0a745f","name":"complete msg object","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1080,"y":120,"wires":[]},{"id":"0dc5f6c75011bcbf","type":"debug","z":"273b95be5f0a745f","name":"msg.parts.index","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"parts.index","targetType":"msg","statusVal":"","statusType":"auto","x":1060,"y":240,"wires":[]},{"id":"8fe9703b7e8a1271","type":"inject","z":"273b95be5f0a745f","name":"","props":[{"p":"payload"},{"p":"index1","v":"2","vt":"num"},{"p":"index2","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abcdef","payloadType":"str","x":110,"y":160,"wires":[["1dd4fa914ea28353"]]},{"id":"dfec8efbf4635efc","type":"change","z":"273b95be5f0a745f","name":"","rules":[{"t":"delete","p":"complete","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":200,"wires":[["b41a0bea9b144b3a"]]},{"id":"7901ee4977a08814","type":"join","z":"273b95be5f0a745f","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1030,"y":180,"wires":[["0eef0a3a2ae1e8f9"]]},{"id":"0eef0a3a2ae1e8f9","type":"debug","z":"273b95be5f0a745f","name":"split messages","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1220,"y":180,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2022 11:10 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/2 "2022-12-04T11:10:52Z")

</div>

I'm confused. To me it looks like your split doesn't do anything. Since there is an index1 and index2 in every split msg.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 11:14 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/3 "2022-12-04T11:14:24Z")

</div>

The split splits abcdef to be joined up into two arrays abc and def

This is a simple cut-down example of Advent of Code solution showing the issue

The index1 and index2 are variable in real life as the messages are not the same length - I've just hard coded them to 2 and 5 for this example

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 December 2022 11:14 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/4 "2022-12-04T11:14:30Z")

</div>

can you show us the output you see (and the matching flow) when you get the out of order condition?

The flow you posted gives me consistent results...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81e41b333144be34106ffa4cef486993221d1c9c.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 11:17 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/5 "2022-12-04T11:17:02Z")

</div>

Sorry 🙂  
The flow give the correct result with the superfluous delete msg.complete - I would like to understand why its needed 🙂 And maybe a better way of doing it as I feel I'm relying on timing rather than logic 🙂

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2022 11:21 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/6 "2022-12-04T11:21:59Z")

</div>

> [@cymplecy](#):
>
> The index1 and index2 are variable in real life as the messages are not the same length - I've just hard coded them to 2 and 5 for this example

But that isn't what your split node is actually doing. Since every message contains both properties, it cannot be ~~split~~ switched correctly.

> [@cymplecy](#):
>
> Advent of Code

Do you have a link for that?

* * *

There is a much simpler switch that will do exactly what you want with no need for a complete message. Let me know if you want a hint.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 December 2022 11:24 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/7 "2022-12-04T11:24:54Z")

</div>

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

Watch the animations in this blog post...

> **[Making flows asynchronous by default](https://nodered.org/blog/2019/08/16/going-async)**
>
> Changes are coming in Node-RED 1.0 to how messages are routed in a flow. Find out what it means to go asynchronous.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 11:33 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/8 "2022-12-04T11:33:49Z")

</div>

> [@TotallyInformation](#):
>
> But that isn't what your split node is actually doing. Since every message contains both properties, it cannot be ~~split~~ switched correctly.

Your concentrating on the wrong part of the flow - that's just test data - don't worry about it 🙂

> [@TotallyInformation](#):
>
> There is a much simpler switch that will do exactly what you want with no need for a complete message.

I'm all ears (as long as not using JS or JSONata 🙂 )

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 11:36 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/9 "2022-12-04T11:36:40Z")

</div>

I knew it was something like that but didn't know exactly what the sequences were 🙂  
So, as long as I've an equal number of nodes in the paths, then they will arrive at the join in order:)

Happy bunny now - thanks 🙂

---

<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:** [4 December 2022 11:51 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/10 "2022-12-04T11:51:13Z")

</div>

> [@cymplecy](#):
>
> as long as I've an equal number of nodes in the paths, then they will arrive at the join in order:

Relying on that is a bit dodgy, even if in practice it does work. It would be better to find a way that works without relying on subtleties of the underlying system.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 12:11 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/11 "2022-12-04T12:11:35Z")

</div>

Well that is true but if NR always works that way (or can be always set to work that way in future) then I can live with it - certainly for now 🙂

I was more worried that it was a timing issue and the time taken for a node to do stuff that was important - glad its not 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2022 12:15 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/12 "2022-12-04T12:15:28Z")

</div>

> [@cymplecy](#):
>
> I'm all ears (as long as not using JS or JSONata 🙂 )

I knew you wouldn't want that. I thought at first you wanted to split every other element of the string - that is slightly more complex. To simply split the first x entries in to 1 output and the rest into another, you need to look further into the msg properties that are added by the split node. One of them contains a number and that is all you need. 😉

Happy to give more clues but I'm guessing you would prefer to work it out yourself?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 12:19 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/13 "2022-12-04T12:19:04Z")

</div>

I can't find a method of telling a split node dynamically how to split - maybe I can't see it in the docs????

---

<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:** [4 December 2022 12:20 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/14 "2022-12-04T12:20:55Z")

</div>

Just for info you can switch and reconstruct sequence, while setting msg.parts.type to array, and join automatically  
e.g.

```auto
[{"id":"8fe9703b7e8a1271","type":"inject","z":"273b95be5f0a745f","name":"","props":[{"p":"payload"},{"p":"index1","v":"2","vt":"num"},{"p":"index2","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abcdef","payloadType":"str","x":290,"y":120,"wires":[["1dd4fa914ea28353"]]},{"id":"1dd4fa914ea28353","type":"split","z":"273b95be5f0a745f","name":"","splt":" 1","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":350,"y":160,"wires":[["db41a23d5301370d"]]},{"id":"db41a23d5301370d","type":"change","z":"273b95be5f0a745f","name":"","rules":[{"t":"set","p":"parts.type","pt":"msg","to":"array","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":160,"wires":[["8cfe1f4b84945ba0"]]},{"id":"8cfe1f4b84945ba0","type":"switch","z":"273b95be5f0a745f","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"lte","v":"index1","vt":"msg"},{"t":"gt","v":"index1","vt":"msg"}],"checkall":"true","repair":true,"outputs":2,"x":670,"y":160,"wires":[["b41a0bea9b144b3a"],["b41a0bea9b144b3a"]]},{"id":"b41a0bea9b144b3a","type":"junction","z":"273b95be5f0a745f","x":820,"y":160,"wires":[["86e060fbfa932ea5","7901ee4977a08814"]]},{"id":"86e060fbfa932ea5","type":"debug","z":"273b95be5f0a745f","name":"complete msg object","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1080,"y":120,"wires":[]},{"id":"7901ee4977a08814","type":"join","z":"273b95be5f0a745f","name":"","mode":"auto","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":970,"y":160,"wires":[["0eef0a3a2ae1e8f9"]]},{"id":"0eef0a3a2ae1e8f9","type":"debug","z":"273b95be5f0a745f","name":"split messages","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1140,"y":160,"wires":[]}]

```

 ![chrome_screenshot_1670156427876](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/1780700465f6ef797db838376c54f4f09a104705.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 12:26 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/15 "2022-12-04T12:26:51Z")

</div>

ooh - I see what you did there - very nice

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

coupled with the slight "cheating" with setting it to array type 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2022 12:39 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/16 "2022-12-04T12:39:01Z")

</div>

> [@cymplecy](#):
>
> I can't find a method of telling a split node dynamically how to split - maybe I can't see it in the docs????

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

> **Example Flow**
>
> ```auto
> [{"id":"1dd4fa914ea28353","type":"split","z":"273b95be5f0a745f","name":"","splt":" 1","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":290,"y":400,"wires":[["8a030eb6ba662cbb"]]},{"id":"8fe9703b7e8a1271","type":"inject","z":"273b95be5f0a745f","name":"","props":[{"p":"payload"},{"p":"index1","v":"2","vt":"num"},{"p":"index2","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abcdef","payloadType":"str","x":170,"y":400,"wires":[["1dd4fa914ea28353"]]},{"id":"8a030eb6ba662cbb","type":"switch","z":"273b95be5f0a745f","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"lte","v":"3","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":410,"y":400,"wires":[["4ad6de13a9c84c5e"],["0806269d72e09fa7"]]},{"id":"4ad6de13a9c84c5e","type":"debug","z":"273b95be5f0a745f","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":380,"wires":[]},{"id":"0806269d72e09fa7","type":"debug","z":"273b95be5f0a745f","name":"debug 30","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":420,"wires":[]}]
> 
> ```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 December 2022 12:39 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/17 "2022-12-04T12:39:57Z")

</div>

I don't think me and you are on the same sheet of paper with this one 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2022 12:42 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/18 "2022-12-04T12:42:39Z")

</div>

You said you wanted `abc` in one output and `def` in the other. To do that, you need to split as normal and then switch on the `msg.parts.index` value so that any index \<4 is output to the top output and everything else goes to the bottom output. Then you can join each one and job's done.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2022 12:51 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/19 "2022-12-04T12:51:46Z")

</div>

Incidentally, the answer to yesterday's challange is much easier in code than it is in nodes. 🙂

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 December 2022 13:01 UTC](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661/20 "2022-12-04T13:01:55Z")

</div>

I agree (can be done with a handful of lines) but I have renewed appreciation for Simon's galient efforts to avoid code. It certainly does highlight areas of improvement for low code nodes.

For example, today's challenge would have been super simple with a good "string" node.

[Next page](https://discourse.nodered.org/t/split-messages-arriving-out-of-order/71661.md?page=2)
