# How to buffer data and output them once the array reach a defined size

**URL:** <https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571>\
**Category:** General\
**Created:** [7 September 2020 22:39 UTC](https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571 "2020-09-07T22:39:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvanoMTL](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sylvanomtl/32/28316_2.png) [@sylvanoMTL](https://discourse.nodered.org/u/sylvanoMTL)\
**Post date:** [7 September 2020 22:39 UTC](https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571/1 "2020-09-07T22:39:40Z")

</div>

I am trying to downsample streamed data I am receiving via MQTT. I wish to build a buffer array and push values/objects to it, then pass the bufferin a payload before a reset.

For instance I want to use a defined number of element.  
Later I want to implement a stop condition (based on a timestamp contained in one of the element)  
Has anyone tried this before?

---

<div class="post-metadata">

**Author:** ![sylvanoMTL](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sylvanomtl/32/28316_2.png) [@sylvanoMTL](https://discourse.nodered.org/u/sylvanoMTL)\
**Post date:** [7 September 2020 23:40 UTC](https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571/2 "2020-09-07T23:40:29Z")

</div>

got the answer to my own question, at least for the first part.

```auto
[{"id":"300dacd9.0ac534","type":"function","z":"c0e2e862.88aff","name":"Build Array","func":"var buffer_size=10; \nif (!context.buffer || context.buffer.length==10) {\n context.buffer = [];\n}\n\nif(context.buffer.length<buffer_size){\n context.buffer.push(msg.payload);\n var t = context.buffer.length;\n node.status({fill:\"blue\",shape:\"ring\",text:t});\n}\n\nif (context.buffer.length==buffer_size){\n msg.payload = context.buffer;\n return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1130,"y":1040,"wires":[["bf118f20.71866"]]},{"id":"166eb1e7.cc22be","type":"inject","z":"c0e2e862.88aff","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":720,"y":980,"wires":[["3aa4a6a7.603702"]]},{"id":"bf118f20.71866","type":"debug","z":"c0e2e862.88aff","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":1080,"wires":[]},{"id":"3aa4a6a7.603702","type":"function","z":"c0e2e862.88aff","name":"Value injected in the array","func":"if(!context.value){\n context.value = 0; \n}\n\n\nif(msg.payload){\n \n msg.payload = context.value;\n node.status({fill:\"blue\",shape:\"ring\",text:context.value});\n context.value++;\n return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":1020,"wires":[["300dacd9.0ac534"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 September 2020 00:49 UTC](https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571/3 "2020-09-08T00:49:12Z")

</div>

I think the `join` node would work.

(I haven't looked at the flow.)

But reading what you say, I'd guess the `join` node.

You can set it to wait for _n_ number of messages then send the new message as a variety of things.  
Strings, arrays, etc.

---

<div class="post-metadata">

**Author:** ![sylvanoMTL](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sylvanomtl/32/28316_2.png) [@sylvanoMTL](https://discourse.nodered.org/u/sylvanoMTL)\
**Post date:** [8 September 2020 09:18 UTC](https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571/4 "2020-09-08T09:18:32Z")

</div>

thank you my `join` node configure as "Manual | each msg.payload | Array | n=10 works exactly as expected.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 September 2020 09:52 UTC](https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571/5 "2020-09-08T09:52:20Z")

</div>

(I'm not too worried, but scroll up to that post and tick the `solution` button.)

That helps others if they have a similar problem find the answer quickly.

BTW, glad to help.

---

<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:** [22 September 2020 09:52 UTC](https://discourse.nodered.org/t/how-to-buffer-data-and-output-them-once-the-array-reach-a-defined-size/32571/6 "2020-09-22T09:52:24Z")

</div>

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