# How to reassemble n sequential messages in one?

**URL:** <https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280>\
**Category:** General\
**Created:** [25 November 2021 15:36 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280 "2021-11-25T15:36:41Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [25 November 2021 15:36 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/1 "2021-11-25T15:36:42Z")

</div>

In my flow (v.1.3.5) I receive sequential MQTT messages that represent fragments of a bigger packet. In the messages I have the info of which number of fragment is. How can I reassemble the fragments in the original packet? What's the best method?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 November 2021 15:44 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/2 "2021-11-25T15:44:26Z")

</div>

> [@thoraz](#):
>
> I have the info of which number of fragment is

Do you also know how many fragments there are? Or does the last one have some flag to let you know? What type of data are the fragments - strings?

Depending on the details you could use the Join node to do this.

If you configure the Join node in manual mode, you'll see a number of options for how it should combine the incoming messages.

You may find you also need to set `msg.parts` on each message containing the right meta data for the Join node to know what to do.

The best thing to do would be to play with the Split node (which does the opposite of the Join node) and can turn a single message into a stream of messages. It sets `msg.parts` on each message in the stream - have a look at that to see what you could try emulating with your incoming mqtt messages.

---

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [25 November 2021 17:44 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/3 "2021-11-25T17:44:14Z")

</div>

> Do you also know how many fragments there are?

Yes. The fragments are basically a json where there are names which values are the number of the actual fragment and the total number of fragments.

There is also a name for the data of fragment which value is a string (I want recompose a huge string that is a base64 image).

I'm a novice with Node-Red but I get what do you mean with Join node. I noticed that there is a "After a number of message parts". I should focus on it?

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [25 November 2021 20:40 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/4 "2021-11-25T20:40:27Z")

</div>

The idea is that you use the `join` node in automatic mode and that you extend the mqtt output (e.g. in `change` node) with a `msg.parts` property so that `join` node knows how to automatically join it.

Here below an example of how the `parts` property in the `msg` must look like:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/88318d1ae2ed74907876d10bb68436ea6dde52e3.png)

---

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [26 November 2021 09:52 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/5 "2021-11-26T09:52:22Z")

</div>

I read about the message sequences and the `msg.parts` and tried to run an example but I can't see difference between the automatic join with and without `parts` property.

My test flow:

 ![test_flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0a469b14deabbe92dc7a6cdc6b42f2985cc17019.png)

My splitted packets which represent my actual packets to reassemble:

![test_flow_splitted_packets](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/672192ae97ef8f878fb4ec0603bbe31f184688ab.png)

I modified the splitted packets with "Change" node to add the `parts` property:

 ![test_flow_change_node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/abec9013dcc02558d8e039856975f41897cdb042.png)  
 ![test_flow_splitted_packets_with_parts](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0ab4bbc4b27887c9d7bee0461dccf49783353404.png)

Here the join of the packets with the `parts` property:

![test_flow_join_with_parts](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/bed337dd9c6d577a32c0651658edb8884b8f503f.png)

And the join of the packets without the `parts` property:

![test_flow_join_without_parts](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/86b136708d5434bc79636469fd09a07b1c714c45.png)

There is no difference. What am I doing wrong?

And another point: if I add splitted packets with different `parts.id` the join continue to output a single array .

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [26 November 2021 10:07 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/6 "2021-11-26T10:07:51Z")

</div>

> [@thoraz](#):
>
> I read about the message sequences and the `msg.parts` and tried to run an example but I can't see difference between the automatic join with and without `parts` property.

You have put the `parts` property in the `msg.payload`, this is not the correct location. you should put it directly in the `msg` =\> so `msg.parts` and not `msg.payload.parts`.  
The join node in automatic node is requiring `msg.parts` so that it knows how to automatically join the parts !

Note that your `split` node will also set `msg.parts` but that is not visible in your debug output as you are only outputting `msg.payload`.

---

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [26 November 2021 10:17 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/7 "2021-11-26T10:17:26Z")

</div>

I got it, now it works! Thanks very much.

Last question: now I have an array with my packets data ordered but how can I concatenate the items (strings) of the array to obtain a single string?

Thanks again.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 November 2021 10:18 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/8 "2021-11-26T10:18:15Z")

</div>

You can tell the Join node to join as a string.

---

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [26 November 2021 10:21 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/9 "2021-11-26T10:21:44Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [26 November 2021 10:57 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/10 "2021-11-26T10:57:51Z")

</div>

Sorry but I miss something. If I want tell to the Join node to join as a string, I need to set it in manual mode and choose "a String" in "to create" field. But if I set the Join node to manual it seems to loss the ordering of packets according to the `parts.index` value, instead concatenate the strings according to the receiving order.

If I use the Join node in automatic mode the ordering is ok but in array with separate items.

What'd I miss?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 November 2021 11:07 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/11 "2021-11-26T11:07:30Z")

</div>

I couldn't remember all the nuances of the manual-vs-automatic mode in the Join node.

If you want to keep the Join node in automatic mode, then you can set:

```auto
msg.parts.type = "string"
msg.parts.ch = ""

```

On each message and the Join node should do what you want.

---

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [26 November 2021 11:15 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/12 "2021-11-26T11:15:47Z")

</div>

It works now! Thank you. I thing to understand the `msg.parts.type` property but what about `msg.parts.ch`? Can you explain it briefly?

Anyway, thank you very much.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 November 2021 11:35 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/13 "2021-11-26T11:35:53Z")

</div>

`msg.parts.ch` is the _character_ used to join the string fragments together. By default it uses `"\n"` as it assumes you have split multiple lines of text into individual lines.

---

<div class="post-metadata">

**Author:** ![thoraz](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thoraz](https://discourse.nodered.org/u/thoraz)\
**Post date:** [29 November 2021 09:39 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/14 "2021-11-29T09:39:12Z")

</div>

Sorry guys but I'm here again cause I tried to apply the test flow above to my actual project but I haven't the same result.

My flow:

 ![Immagine 2021-11-29 101108](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3b6cbb61c68829d779b22fdc90ec08579156118.png)

and my outputs (the first one the packet changed and the second one the packet joined):

 ![Immagine 2021-11-29 101258](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/07d456b5c9823b92b510713864d086a2fc69df1f.png)  
 ![Immagine 2021-11-29 101533](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80c64d1a30fa0e7968c84cc4fd2e409d1804c504.png)

As you can see, the joined packet is a string with `[object Object]` repeated for every packet incoming. I've used the same teory of the test flow, the only difference is that now I receive the packets from a mqtt and json nodes. Where did the `[object Object]` come from?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [29 November 2021 10:37 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/15 "2021-11-29T10:37:06Z")

</div>

Why are you passing it through a JSON node? I thought each message contained a partial fragment of JSON that you needed to recombine into a single string before it could be parsed.

The problem comes because you are converting each JSON string to an Object, then telling the Join node to join them as Strings. And if you `toString` an Object you get `[object Object]`.

---

<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:** [13 December 2021 10:38 UTC](https://discourse.nodered.org/t/how-to-reassemble-n-sequential-messages-in-one/54280/16 "2021-12-13T10:38:01Z")

</div>

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