# Count messages in rolling window?

**URL:** <https://discourse.nodered.org/t/count-messages-in-rolling-window/10691>\
**Category:** General\
**Created:** [30 April 2019 19:05 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691 "2019-04-30T19:05:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [30 April 2019 19:05 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691/1 "2019-04-30T19:05:15Z")

</div>

Hello,

I'm looking to count the number of messages in a rolling window, i.e. 15 minutes and output the number when a new message is received, is this possible with nodes, or would I need a custom function?

I've tried to write a simple function node but sadly couldn't achieve what I wanted...

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [30 April 2019 19:14 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691/2 "2019-04-30T19:14:55Z")

</div>

did you look at [https://flows.nodered.org/node/node-red-contrib-msg-speed](https://flows.nodered.org/node/node-red-contrib-msg-speed) ?

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [30 April 2019 19:22 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691/3 "2019-04-30T19:22:09Z")

</div>

@ukmoose I did but assumed it was suitable, on second glance it might just be...

I was keen to understand how I could do this via a function node, but will have a look at the node now and see how it works out. Appreciate the heads up.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 April 2019 19:46 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691/4 "2019-04-30T19:46:06Z")

</div>

Well I’m sure the source is available so you could study it as well to see how/if it works

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [30 April 2019 19:47 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691/5 "2019-04-30T19:47:15Z")

</div>

On it now actually, just learning about buffers, looks to be pretty straightforward...

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [30 April 2019 21:42 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691/6 "2019-04-30T21:42:58Z")

</div>

Hi @swiftnesses,

As @ukmoose already mentioned, you could indeed have a look at the [code](https://github.com/bartbutenaers/node-red-contrib-msg-speed/blob/master/msg_speed.js#L18) of my speed node. It makes use of the [circular-buffer](https://www.npmjs.com/package/circular-buffer) NPM module.

Summarized:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/993113804437172f10a3e9213c69ab21af286f78.png)

1. I create a ring buffer, with length = 1 cell for each second. E.g. when the user specifies "speed per _ **minute** _", then I create 60 cells.
2. Every second I count the number of messages that have arrived during the last second, and I store that count into the next cell.
3. Then I move the pointer to the next cell.
4. That process continues every second. After 1 minute you will start overwriting old values ...

So at any moment the **sum** of the values in ALL cells = total number of messages arrived during the last minute. When you divide that sum by 60, you have the average number of messages per second.

However, calculating the sum of all cells every second would be very inefficient. So I use a simple **trick** to calculate the sum:

1. As mentioned above, the messages are counted every second.
2. Then the sum is recalculated, based on that new count and the old count (which is stored currently in the cell and will be overwritten in the next step):  
`sum = sum - old_counter + new_counter`
3. Store the new counter in the cell

So I remove old counters from the sum and add new counters to the sum. That way the sum always contains the sum of all cells, without having to loop all the cells. This is particulary interesting when you have a large number of cells ...  
Bart

---

<div class="post-metadata">

**Author:** ![swiftnesses](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@swiftnesses](https://discourse.nodered.org/u/swiftnesses)\
**Post date:** [1 May 2019 06:44 UTC](https://discourse.nodered.org/t/count-messages-in-rolling-window/10691/7 "2019-05-01T06:44:03Z")

</div>

@BartButenaers That's very useful information, appreciate you taking the time to explain the logic!

I've implemented a rolling buffer now, but will take a look at circular-buffer too.
