# Joining 2 buffer payloads in java script

**URL:** <https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483>\
**Category:** General\
**Created:** [10 May 2022 22:59 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483 "2022-05-10T22:59:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [10 May 2022 22:59 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/1 "2022-05-10T22:59:10Z")

</div>

I am trying to duplicate the join node in javascript as a learning exercise. I am trying to join a payload consisting of 2 buffers. I have a test env of:

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

My function node is:

```auto
var a= Buffer.from(msg.payload);
var b = Buffer.from(msg.payload);
var buf = Buffer.concat([a,b]);
msg = {payload: buf};
return msg;

```

I am getting the following results:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/e/0ef65893ba085633c8c698349f7d4a74b2526a2c.png)

I suspect my function is only processing var a for each inject node and not waiting for both. I wonder if I need a loop function in order to pick up var b.

Here is the code for reference.

```auto
[{"id":"7853b54d4b445e37","type":"function","z":"4512ac95.5bc124","name":"","func":"var a= Buffer.from(msg.payload);\nvar b = Buffer.from(msg.payload);\nvar buf = Buffer.concat([a,b]);\nmsg = {payload: buf};\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":820,"wires":[["270630966f5476f3"]]},{"id":"270630966f5476f3","type":"debug","z":"4512ac95.5bc124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":820,"wires":[]},{"id":"0a6178d1c6bfd378","type":"inject","z":"4512ac95.5bc124","name":"a","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[82,49,124,48,124,109,101,116,101,114,32,49,46,115,114,99,61,67,79,68,45,35,49,46,110,117,109,61,49,35,49,46,110,97,109,61,77,73,67,80,69,65,75,35,49,46,108,111,119,61,45,49,53,48,46,48,35,49,46,104,105,61,50,48,46,48,35,49,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,77,73,67,32,111,117,116,112,117,116,32,105,110,32,67,79,68,69,67,35,49,46,117,110,105,116,61,100,66,70,83,35,49,46,102,112,115,61,52,48,35,50,46,115,114,99,61,67,79,68,45,35,50,46,110,117,109,61,50,35,50,46,110,97,109,61,77,73,67,35,50,46,108,111,119,61,45,49,53,48,46,48,35,50,46,104,105,61,50,48,46,48,35,50,46,100,101,115,99,61,65,118,101,114,97,103,101,32,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,77,73,67,32,111,117,112,117,116,32,105,110,32,67,79,68,69,67,35,50,46,117,110,105,116,61,100,66,70,83,35,50,46,102,112,115,61,50,48,35,51,46,115,114,99,61,84,88,45,35,51,46,110,117,109,61,53,35,51,46,110,97,109,61,72,87,65,76,67,35,51,46,108,111,119,61,45,49,53,48,46,48,35,51,46,104,105,61,50,48,46,48,35,51,46,100,101,115,99,61,86,111,108,116,97,103,101,32,112,114,101,115,101,110,116,32,97,116,32,116,104,101,32,72,97,114,100,119,97,114,101,32,65,76,67,32,82,67,65,32,80,108,117,103,35,51,46,117,110,105,116,61,100,66,70,83,35,51,46,102,112,115,61,50,48,35,52,46,115,114,99,61,82,65,68,35,52,46,110,117,109,61,51,51,52,35,52,46,110,97,109,61,43,49,51,46,56,65,35,52,46,108,111,119,61,49,48,46,53,35,52,46,104,105,61,49,53,46,48,35,52,46,100,101,115,99,61,77,97,105,110,32,114,97,100,105,111,32,105,110,112,117,116,32,118,111,108,116,97,103,101,32,97,116,32,80,65,35,52,46,117,110,105,116,61,86,111,108,116,115,35,52,46,102,112,115,61,48,35,53,46,115,114,99,61,82,65,68,35,53,46,110,117,109,61,48,35,53,46,110,97,109,61,43,49,51,46,56,66,35,53,46,108,111,119,61,49,48,46,53,35,53,46,104,105,61,49,53,46,48,35,53,46,100,101,115,99,61,77,97,105,110,32,114,97,100,105,111,32,105,110,112,117,116,32,118,111,108,116,97,103,101,32,97,116,32,67,80,85,32,40,99,111,110,116,105,110,117,111,117,115,41,35,53,46,117,110,105,116,61,86,111,108,116,115,35,53,46,102,112,115,61,48,35,54,46,115,114,99,61,82,65,68,35,54,46,110,117,109,61,51,35,54,46,110,97,109,61,77,65,73,78,70,65,78,35,54,46,108,111,119,61,48,46,48,35,54,46,104,105,61,56,49,57,50,46,48,35,54,46,100,101,115,99,61,77,97,105,110,32,114,97,100,105,111,32,102,97,110,32,82,80,77,35,54,46,117,110,105,116,61,82,80,77,35,54,46,102,112,115,61,48,35,55,46,115,114,99,61,84,88,45,35,55,46,110,117,109,61,49,35,55,46,110,97,109,61,70,87,68,80,87,82,35,55,46,108,111,119,61,48,46,48,35,55,46,104,105,61,53,51,46,48,35,55,46,100,101,115,99,61,82,70,32,80,111,119,101,114,32,70,111,114,119,97,114,100,35,55,46,117,110,105,116,61,100,66,109,35,55,46,102,112,115,61,50,48,35,56,46,115,114,99,61,84,88,45,35,56,46,110,117,109,61,50,35,56,46,110,97,109,61,82,69,70,80,87,82,35,56,46,108,111,119,61,48,46,48,35,56,46,104,105,61,53,51,46,48,35,56,46,100,101,115,99,61,82,70,32,80,111,119,101,114,32,82,101,102,108,101,99,116,101,100,35,56,46,117,110,105,116,61,100,66,109,35,56,46,102,112,115,61,50,48,35,57,46,115,114,99,61,84,88,45,35,57,46,110,117,109,61,51,35,57,46,110,97,109,61,83,87,82,35,57,46,108,111,119,61,49,46,48,35,57,46,104,105,61,57,57,57,46,48,35,57,46,100,101,115,99,61,82,70,32,83,87,82,35,57,46,117,110,105,116,61,83,87,82,35,57,46,102,112,115,61,50,48,35,49,48,46,115,114,99,61,84,88,45,35,49,48,46,110,117,109,61,52,35,49,48,46,110,97,109,61,80,65,84,69,77,80,35,49,48,46,108,111,119,61,48,46,48,35,49,48,46,104,105,61,49,48,48,46,48,35,49,48,46,100,101,115,99,61,80,65,32,84,101,109,112,101,114,97,116,117,114,101,35,49,48,46,117,110,105,116,61,100,101,103,67,35,49,48,46,102,112,115,61,48,35,49,49,46,115,114,99,61,83,76,67,35,49,49,46,110,117,109,61,48,35,49,49,46,110,97,109,61,50,52,107,72,122,35,49,49,46,108,111,119,61,45,49,52,48,46,48,35,49,49,46,104,105,61,50,48,46,48,35,49,49,46,100,101,115,99,61,50,52,107,72,122,32,98,114,111,97,100,98,97,110,100,32,115,108,105,99,101,32,114,101,99,101,105,118,101,114,32,115,105,103,110,97,108,32,108,101,118,101,108,35,49,49,46,117,110,105,116,61,100,66,70,83,35,49,49,46,102,112,115,61,49,48,35,49,50,46,115,114,99,61,83,76,67,35,49,50,46,110,117,109,61,48,35,49,50,46,110,97,109,61,79,83,67,35,49,50,46,108,111,119,61,45,49,52,48,46,48,35,49,50,46,104,105,61,50,48,46,48,35,49,50,46,100,101,115,99,61,83,105,103,110,97,108,32,83,116,114,101,110,103,116,104,32,97,102,116,101,114,32,79,83,67,35,49,50,46,117,110,105,116,61,100,66,70,83,35,49,50,46,102,112,115,61,49,48,35,49,51,46,115,114,99,61,83,76,67,35,49,51,46,110,117,109,61,48,35,49,51,46,110,97,109,61,76,69,86,69,76,35,49,51,46,108,111,119,61,45,49,53,48,46,48,35,49,51,46,104,105,61,50,48,46,48,35,49,51,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,115,105,103,110,97,108,115,32,105,110,32,116,104,101,32,102,105,108,116,101,114,32,112,97,115,115,98,97,110,100,35,49,51,46,117,110,105,116,61,100,66,109,35,49,51,46,102,112,115,61,49,48,35,49,52,46,115,114,99,61,83,76,67,35,49,52,46,110,117,109,61,48,35,49,52,46,110,97,109,61,78,82,47,65,78,70,35,49,52,46,108,111,119,61,45,49,53,48,46,48,35,49,52,46,104,105,61,50,48,46,48,35,49,52,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,97,102,116,101,114,32,78,82,47,65,78,70,35,49,52,46,117,110,105,116,61,100,66,109,35,49,52,46,102,112,115,61,49,48,35,49,53,46,115,114,99,61,83,76,67,35,49,53,46,110,117,109,61,48,35,49,53,46,110,97,109,61,65,71,67,43,35,49,53,46,108,111,119,61,45,49,53,48,46,48,35,49,53,46,104,105,61,48,46,48,35,49,53,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,97,102,116,101,114,32,65,71,67,35,49,53,46,117,110,105,116,61,100,66,70,83,35,49,53,46,102,112,115,61,49,48,35,49,54,46,115,114,99,61,84,88,45,35,49,54,46,110,117,109,61,56,35,49,54,46,110,97,109,61,67,79,68,69,67,35,49,54,46,108,111,119,61,45,49,53,48,46,48,35,49,54,46,104,105,61,50,48,46,48,35,49,54,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,67,79,68,69,67,32,111,117,116,112,117,116,35,49,54,46,117,110,105,116,61,100,66,70,83,35,49,54,46,102,112,115,61,49,48,35,49,55,46,115,114,99,61,84,88,45,35,49,55,46,110,117,109,61,56,35,49,55,46,110,97,109,61,83,67,95,77,73,67,35,49,55,46,108,111,119,61,45,49,53,48,46,48,35,49,55,46,104,105,61,50,48,46,48,35,49,55,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,77,73,67,32,111,117,116,112,117,116,35,49,55,46,117,110,105,116,61,100,66,70,83,35,49,55,46,102,112,115,61,49,48,35,49,56,46,115,114,99,61,84,88,45,35,49,56,46,110,117,109,61,56,35,49,56,46,110,97,109,61,67,79,77,80,80,69,65,75,35,49,56,46,108,111,119,61,45,49,53,48,46,48,35,49,56,46,104,105,61,50,48,46,48,35,49,56,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,115,105,103,110,97,108,115,32,97,102,116,101,114,32,116,104,101,32,67,76,73,80,80,69,82,32,40,67,111,109,112,114,101,115,115,105,111,110,41,35,49,56,46,117,110,105,116,61,100,66,70,83,35,49,56,46,102,112,115,61,50,48,35,49,57,46,115,114,99,61,84,88,45,35,49,57,46,110,117,109,61,56,35,49,57,46,110,97,109,61,83,67,95,70,73,76,84,95,49,35,49,57,46,108,111,119,61,45,49,53,48,46,48,35,49,57,46,104,105,61,50,48,46,48,35,49,57,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,97,102,116,101,114,32,70,105,108,116,101,114,32,49,35,49,57,46,117,110,105,116,61,100,66,70,83,35,49,57,46,102,112,115,61,49,48,35,50,48,46,115,114,99,61,84,88,45,35,50,48,46,110,117,109,61,56,35,50,48,46,110,97,109,61,65,76,67,35,50,48,46,108,111,119,61,45,49,53,48,46,48,35,50,48,46,104,105,61,50,48,46,48,35,50,48,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,115,105,103,110,97,108,115,32,97,102,116,101,114,32,83,87,32,65,76,67,32,40,83,83,66,32,80,101,97,107,41,35,50,48,46,117,110,105,116,61,100,66,70,83,35,50,48,46,102,112,115,61,49,48,35,50,49,46,115,114,99,61,84,88,45,35,50,49,46,110,117,109,61,56,35,50,49,46,110,97,109,61,80,82,69,95,87,65,86,69,95,65,71,67,35,50,49,46,108,111,119,61,45,49,53,48,46,48,35,50,49,46,104,105,61,50,48,46,48,35,50,49,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,98,101,102,111,114,101,32,87,97,118,101,102,111,114,109,32,65,71,67,35,50,49,46,117,110,105,116,61,100,66,70,83,35,50,49,46,102,112,115,61,49,48]","payloadType":"bin","x":210,"y":800,"wires":[["7853b54d4b445e37"]]},{"id":"e9720adfb13ce577","type":"inject","z":"4512ac95.5bc124","name":"b","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[45,35,50,53,46,110,117,109,61,56,35,50,53,46,110,97,109,61,65,70,82,65,77,80,35,50,53,46,108,111,119,61,45,49,53,48,46,48,35,50,53,46,104,105,61,50,48,46,48,35,50,53,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,115,105,103,110,97,108,115,32,97,102,116,101,114,32,116,104,101,32,114,97,109,112,35,50,53,46,117,110,105,116,61,100,66,70,83,35,50,53,46,102,112,115,61,49,48,35,50,54,46,115,114,99,61,84,88,45,35,50,54,46,110,117,109,61,56,35,50,54,46,110,97,109,61,80,79,83,84,95,80,35,50,54,46,108,111,119,61,45,49,53,48,46,48,35,50,54,46,104,105,61,48,46,48,35,50,54,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,97,102,116,101,114,32,97,108,108,32,112,114,111,99,101,115,115,105,110,103,32,105,115,32,100,111,110,101,32,98,117,116,32,98,101,102,111,114,101,32,97,116,116,101,110,117,97,116,105,111,110,32,102,111,114,32,112,111,119,101,114,32,99,111,110,116,114,111,108,35,50,54,46,117,110,105,116,61,100,66,70,83,35,50,54,46,102,112,115,61,49,48,35,50,55,46,115,114,99,61,84,88,45,35,50,55,46,110,117,109,61,56,35,50,55,46,110,97,109,61,71,65,73,78,35,50,55,46,108,111,119,61,45,49,53,48,46,48,35,50,55,46,104,105,61,48,46,48,35,50,55,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,97,102,116,101,114,32,71,97,105,110,35,50,55,46,117,110,105,116,61,100,66,70,83,35,50,55,46,102,112,115,61,49,48,35,10,45,35,50,53,46,110,117,109,61,56,35,50,53,46,110,97,109,61,65,70,82,65,77,80,35,50,53,46,108,111,119,61,45,49,53,48,46,48,35,50,53,46,104,105,61,50,48,46,48,35,50,53,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,111,102,32,115,105,103,110,97,108,115,32,97,102,116,101,114,32,116,104,101,32,114,97,109,112,35,50,53,46,117,110,105,116,61,100,66,70,83,35,50,53,46,102,112,115,61,49,48,35,50,54,46,115,114,99,61,84,88,45,35,50,54,46,110,117,109,61,56,35,50,54,46,110,97,109,61,80,79,83,84,95,80,35,50,54,46,108,111,119,61,45,49,53,48,46,48,35,50,54,46,104,105,61,48,46,48,35,50,54,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,97,102,116,101,114,32,97,108,108,32,112,114,111,99,101,115,115,105,110,103,32,105,115,32,100,111,110,101,32,98,117,116,32,98,101,102,111,114,101,32,97,116,116,101,110,117,97,116,105,111,110,32,102,111,114,32,112,111,119,101,114,32,99,111,110,116,114,111,108,35,50,54,46,117,110,105,116,61,100,66,70,83,35,50,54,46,102,112,115,61,49,48,35,50,55,46,115,114,99,61,84,88,45,35,50,55,46,110,117,109,61,56,35,50,55,46,110,97,109,61,71,65,73,78,35,50,55,46,108,111,119,61,45,49,53,48,46,48,35,50,55,46,104,105,61,48,46,48,35,50,55,46,100,101,115,99,61,83,105,103,110,97,108,32,115,116,114,101,110,103,116,104,32,97,102,116,101,114,32,71,97,105,110,35,50,55,46,117,110,105,116,61,100,66,70,83,35,50,55,46,102,112,115,61,49,48,35,10]","payloadType":"bin","x":210,"y":860,"wires":[["7853b54d4b445e37"]]}]

```

Regards

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 May 2022 23:33 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/2 "2022-05-10T23:33:52Z")

</div>

> [@Mick](#):
>
> I suspect my function is only processing var a for each inject node and not waiting for both.

That's exactly right, it's flow based programming: a message arrives at a node, is processed, then it's gone.  
Your function has no knowledge of previous messsages.

There are two ways to deal with it:

1. Use context storage to retain the previous payload - assuming you can tell if a given message is type a or type b.
2. Use a Join node to combine the two messages into one. This will require msg.topic to distinguish a from b messages.

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [10 May 2022 23:36 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/3 "2022-05-10T23:36:00Z")

</div>

Thanks for the confirmation. What I have been attempting to do form the onset is duplicate the join node. You can select how many msgs it collects before the join. I am just trying to do this in java script as an exercise.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 May 2022 23:42 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/4 "2022-05-10T23:42:01Z")

</div>

Avoiding a Join node then, your context variable could be an array.  
Either push the latest value to the array or else concatenate all the array elements and pass the result on.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [10 May 2022 23:42 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/5 "2022-05-10T23:42:39Z")

</div>

as jbudd mentioned, you need to use [Context](https://nodered.org/docs/user-guide/writing-functions#storing-data) .. which is like a higher scope of memory to remember each previous payload before concat and some sort of counter that resets after 2 messages.

maybe something like this ?

```auto
// read count from Context or set to 1
let count = context.get("count") || 1

if (count == 1) {
    context.set("a", Buffer.from(msg.payload));
    context.set("count", 2)
}

if (count == 2) {
    context.set("b", Buffer.from(msg.payload));
    context.set("count", 1) // reset counter

    let a = context.get("a");
    let b = context.get("b");

    let buf = Buffer.concat([a, b]);
    msg = { payload: buf };
    return msg;

}

```

---

<div class="post-metadata">

**Author:** ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)\
**Post date:** [10 May 2022 23:44 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/6 "2022-05-10T23:44:38Z")

</div>

Thank you. I did not realize context could be used like this.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [10 May 2022 23:47 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/7 "2022-05-10T23:47:49Z")

</div>

but there is a problem with the code .. if you trigger the same inject node twice .. it still adds the buffers  
maybe thats not what you need ? .. if not you need to do additional checks by possibly sending a msg.topic to differentiate the two separate msgs

ps. its good for coding practice but personally i would stick with the Join node .. its much more powerful

---

<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:** [24 May 2022 23:47 UTC](https://discourse.nodered.org/t/joining-2-buffer-payloads-in-java-script/62483/8 "2022-05-24T23:47:56Z")

</div>

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