# Can the Delay node rate limit, but pass first message immediately, and always pass on the latest message?

**URL:** <https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170>\
**Category:** General\
**Created:** [31 August 2022 08:34 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170 "2022-08-31T08:34:55Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [31 August 2022 11:34 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/22 "2022-08-31T11:34:10Z")

</div>

How come I'd never seen that "Then send the latest message" option? 🤔

What does the Filter contribute? It seems to work according to specification without it?

---

<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:** [31 August 2022 11:38 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/23 "2022-08-31T11:38:00Z")

</div>

if you just send one message it will get repeated without the filter.

---

<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:** [31 August 2022 12:25 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/25 "2022-08-31T12:25:46Z")

</div>

> [@dceejay](#):
>
> flush then reset doesn't make sense (to me) - as once flushed it is in the same state as reset (IE nothing in Q) - so the reset is superfluous.

If there are multiple messages in the queue (which have been added with toFront) then `flush: 1` sends the latest and the reset clears out the rest.

---

<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:** [31 August 2022 12:41 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/26 "2022-08-31T12:41:11Z")

</div>

> [@dceejay](#):
>
> a simple trigger node and filter may work.

Yes, I think it does, provided the payload is a value or a simple object (otherwise the filter doesn't like it). I think this will do the job, which is to help with [Two way switch infinite loop](https://discourse.nodered.org/t/two-way-switch-infinite-loop/67115)

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:** [31 August 2022 12:44 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/27 "2022-08-31T12:44:23Z")

</div>

> [@jbudd](#):
>
> My best attempt:

I can see a problem with those methods, if two messages arrive in quick succession then the second one will get through before the gate is closed.  
It is almost impossible to handle race conditions like that without using semaphore-like techniques to ensure the loop gets closed before the next one gets in.

---

<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:** [31 August 2022 13:02 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/28 "2022-08-31T13:02:06Z")

</div>

Yes, you could insert a rate limit eg 1 message per 20ms but I doubt if that would guarantee that the gate isn't left open too long.

---

<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:** [31 August 2022 13:18 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/29 "2022-08-31T13:18:49Z")

</div>

How about DCJ's solution but set the filter to block unless msg.\_msgid changes? I think that removes the issue of filtering out genuine repeated messages.

---

<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:** [31 August 2022 13:22 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/30 "2022-08-31T13:22:50Z")

</div>

> [@jbudd](#):
>
> you could insert a rate limit eg 1 message per 20ms

There is another, related problem, imagine that there is a message waiting in the gate node, another in the rate limit node, and the Set Mode trigger is about to run down. It is possible that the trigger will run down, open the queue releasing the message in the gate and if the rate limit node releases the message at that moment then that one will get straight through as the gate is still open.

---

<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:** [31 August 2022 13:25 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/31 "2022-08-31T13:25:58Z")

</div>

> [@jbudd](#):
>
> set the filter to block unless msg.\_msgid changes? I think that removes the issue of filtering out genuine repeated messages.

In fact, for the application here, it doesn't matter if genuine repeated messages get lost.  
Is msg.\_msgid guaranteed unique? I suspect that if you `RED.util.cloneMessage()` a message it may end up with the same id. Other than that, though, yes it should work.

---

<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:** [31 August 2022 21:40 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/32 "2022-08-31T21:40:31Z")

</div>

Were any of mine any good?

---

<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:** [1 September 2022 06:18 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/33 "2022-09-01T06:18:20Z")

</div>

Did any of them satisfy all of the requirements?

---

<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:** [1 September 2022 08:59 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/34 "2022-09-01T08:59:28Z")

</div>

That's a rhetorical question - yes?

I don't know. I've asked for clarity and haven't seen an answer to it.

---

<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:** [1 September 2022 09:17 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/35 "2022-09-01T09:17:08Z")

</div>

> [@Trying\_to\_learn](#):
>
> I've asked for clarity and haven't seen an answer to it.

Sorry, I didn't realise I had missed a request for more information. Which aspect is not clear?

---

<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:** [1 September 2022 09:22 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/36 "2022-09-01T09:22:11Z")

</div>

I read it that "ideally" there would be 3 (maybe 4) message received in a short time.

The first one is sent on directly.  
The second one - if received in less than 5 seconds - is dropped.  
The third - and subsequent - are then on sent at 5 second intervals.

To what limit?  
Can it be "ongoing" forever?  
Or to a nominal of.... 10?

What is supposed to happen to the messages? (after the nominal 10)

Just some of the flows I saw didn't seem to allow for more than 3 messages to arrive and handle more than that.

Yes, I could be wrong. Usually I am, but that's another chapter, so I was making flows to deal with a near infinite stream of messages.  
And - just out of interest - was wondering how well I parsed the request.  
It is not of any help to me or anyone else if I am answering the wrong question or don't understand the one asked.

---

<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:** [1 September 2022 09:34 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/37 "2022-09-01T09:34:48Z")

</div>

> [@Trying\_to\_learn](#):
>
> I read it that "ideally" there would be 3 (maybe 4) message received in a short time.

As I said in the first post "I have a scenario with messages coming in at random intervals". There is no ideal sequence, there could be many close together, or none for hours.

> [@Trying\_to\_learn](#):
>
> The first one is sent on directly.  
> The second one - if received in less than 5 seconds - is dropped.

Both correct.

> [@Trying\_to\_learn](#):
>
> The third - and subsequent - are then on sent at 5 second intervals.

What gave you that idea? Requirement 3 states "If a third (and subsequent) message comes in when there is already one queued then it replaces the one already in the queue". Therefore there is never more than one in the queue. When the five seconds is up then the one and only message in the queue will be released.

---

<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:** [1 September 2022 09:38 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/38 "2022-09-01T09:38:10Z")

</div>

I can't read.

Hey, tell me something I don't know.

Ok.

---

<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:** [1 September 2022 09:46 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/39 "2022-09-01T09:46:50Z")

</div>

Point 3 could have been made a bit clearer, it should probably have been  
3. If a third (and subsequent) message comes in when there is already one queued then it replaces the one already in the queue and will be released at the end of the period, unless yet another message appears within the five seconds.

---

<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:** [1 September 2022 09:53 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/40 "2022-09-01T09:53:34Z")

</div>

> [@Colin](#):
>
> If a third (and subsequent) message comes in when there is already one queued then it replaces the one already in the queue and will be released at the end of the period, unless yet another message appears within the five seconds.

So, there is a message in the queue. It is waiting for the `5` seconds.  
A _third_ message comes in.

The new message replaces the one in the queue - still waiting for the `5` seconds to pass.  
4 seconds after that ANOTHER message arrives.

That replaces the message in the queue. `4` seconds have passed. So does the timeout start again or wait for the original remaining `1` second?

If in that remaining time, YET ANOTHER message arrives.

When does the message finally get spat out?

To me, there is ambiguity there.

Ah, I am (third time today) overthinking things.  
I'm not helping so I shouldn't spend more time on it.

---

<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:** [1 September 2022 10:12 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/41 "2022-09-01T10:12:32Z")

</div>

The timer is not restarted by subsequent messages within the five seconds. Then the timer is restarted by the next message (which is the first message of the next set). So any message in the queue is released five seconds after the first one.  
In fact you are right, there is a condition that I had not defined, and that is that when the five seconds is up and it releases a message it should restart the timer again. The flow using the trigger node does not do this. More thought required.

---

<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:** [1 September 2022 10:14 UTC](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170/42 "2022-09-01T10:14:16Z")

</div>

Nah, I'm way out of my depth.

The one with the `qgate` is probably the one I would put my money on as being best matching and that trick with the queue limit that I missed.

[Previous page](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170.md?page=1)

[Next page](https://discourse.nodered.org/t/can-the-delay-node-rate-limit-but-pass-first-message-immediately-and-always-pass-on-the-latest-message/67170.md?page=3)
