# On-demand delay node

**URL:** <https://discourse.nodered.org/t/on-demand-delay-node/88466>\
**Category:** Feature Requests\
**Tags:** delay\
**Created:** [3 June 2024 11:52 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466 "2024-06-03T11:52:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [3 June 2024 11:52 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/1 "2024-06-03T11:52:42Z")

</div>

I have a sequence of task parameters.  
For executting the task, i use a function node, which cache the sequence in the node context and send the first element of the cache on demand (via `array.shift()`).

So this function node works a bit like the delay node. i thought, this is could be an additional mode of the delay node. What do you think?

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdc33c002aef567981b5851ca7bbed1bb451f4eb.png)

```auto
[{"id":"67fe4939cb96aaea","type":"function","z":"e6998ea910456df2","name":"cache","func":"let payload = msg.payload\n\n\nfunction send_element() {\n let cache = context.get(\"cache\");\n const topic = context.get(\"original_topic\") || msg.topic;\n delete msg.original_topic\n if (cache.length == 0) {\n node.status({ fill: \"red\", shape: \"dot\", text: \"cache empty!\" });\n context.set(\"original_topic\",undefined)\n return\n }\n msg.payload = cache.shift()\n msg.topic = topic\n node.status({ fill: \"green\", shape: \"dot\", text: cache.length });\n node.send(msg);\n}\n\n\n\nif (msg.topic==\"save\") {\n context.set(\"cache\", payload);\n context.set(\"original_topic\",msg.original_topic)\n send_element()\n\n}\n\n\nif (msg.topic == \"get\") {\n send_element()\n}\n\n\nif (msg.topic == \"delete\") {\n context.set(\"cache\", []);\n node.status({ fill: \"red\", shape: \"dot\", text: 0 });\n return\n}\n\n\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"// Der Code hier wird ausgeführt,\n// wenn der Node gestartet wird\nnode.status({ fill: \"red\", shape: \"dot\", text: \"cache empty!\" });","finalize":"","libs":[],"x":770,"y":4480,"wires":[["a45a8074af69afea"]]},{"id":"a45a8074af69afea","type":"delay","z":"e6998ea910456df2","name":"long_running_task","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":630,"y":4600,"wires":[["59f3d2bd842b10f3","5b026e831436d2e0"]]},{"id":"59f3d2bd842b10f3","type":"change","z":"e6998ea910456df2","name":"get","rules":[{"t":"set","p":"topic","pt":"msg","to":"get","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":4480,"wires":[["67fe4939cb96aaea"]]},{"id":"67e03d32e8c4c72f","type":"inject","z":"e6998ea910456df2","name":"Add Tasks","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"original_topic","v":"original_topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"save","payload":"[1,2,3,4,5,6,7,8,9,10]","payloadType":"jsonata","x":540,"y":4380,"wires":[["67fe4939cb96aaea"]]},{"id":"5b026e831436d2e0","type":"debug","z":"e6998ea910456df2","name":"debug 382","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":4600,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 June 2024 13:59 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/2 "2024-06-03T13:59:14Z")

</div>

Hi @kitori

> [@kitori](#):
>
> So this function node works a bit like the delay node. i thought, this is could be an additional mode of the delay node. What do you think?

Sorry, unless I don't get it, this is already possible.  
There are also Flows & Nodes developed to aid message control

We always appreciate input, but unless I am missing something, the mechanics are already there for this, and ready to use?

 ![Screenshot 2024-06-03 at 14.54.51](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/9895285065d5f3e9af9ac1a5cdb60f75a1b4a739.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 June 2024 14:39 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/3 "2024-06-03T14:39:32Z")

</div>

> [@marcus-j-davies](#):
>
> Sorry, unless I don't get it, this is already possible.

It is not possible on a per topic basis to flush messages, according to my tinkering with this node. I may have misunderstood the idea also.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [3 June 2024 18:55 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/4 "2024-06-03T18:55:27Z")

</div>

Do you want to create a queue? I cannot remember if Node-RED has a node for but the request is valid. I use a sort of queue for my messages.

---

<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:** [3 June 2024 20:48 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/5 "2024-06-03T20:48:59Z")

</div>

Correct. The delay node can be used as a queue, but only holds one array of messages so you can’t flush on a per topic basis (currently). That could be an enhancement, but I can’t think of how to implement it simply within the current node structure.

---

<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:** [4 June 2024 01:34 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/6 "2024-06-04T01:34:42Z")

</div>

Would this work for you?

> **[node-red-contrib-ring-buffer](https://flows.nodered.org/node/node-red-contrib-ring-buffer)**
>
> a ring buffer node for node-red

Or this one:

> **[node-red-contrib-queue-gate](https://flows.nodered.org/node/node-red-contrib-queue-gate)**
>
> A gate node (with queueing) for node-RED

(Sorry)

Or this one which is a development of the second one I mentioned:

> **[node-red-contrib-multiple-queue](https://flows.nodered.org/node/node-red-contrib-multiple-queue)**
>
> A multiple message queue node for node-RED

---

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [4 June 2024 06:40 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/7 "2024-06-04T06:40:01Z")

</div>

In the german translation, there is no hint that you can flush single messages \>-\<

So the delay node works perfecly fine if i set the delay high enough 🙂

---

<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:** [4 June 2024 08:39 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/8 "2024-06-04T08:39:39Z")

</div>

> [@kitori](#):
>
> In the german translation, there is no hint that you can flush single messages

Assuming you are talking here about the delay node, the English is  
**flush**  
If the received message has this property set to a numeric value then that many messages will be released immediately. If set to any other type (e.g. boolean), then all outstanding messages held by the node are sent immediately.

So messages can be flushed one at a time by setting msg.flush to 1.

This should do what you want

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8cb6620205cc924c4a7dea15720a603cddb03d32.png)

Open the Comment node to see more details off how to use it.

```auto
[{"id":"b6630ded2db7d680","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":480,"wires":[["ed63ee4225312b40"]]},{"id":"ed63ee4225312b40","type":"delay","z":"bdd7be38.d3b55","name":"Queue","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":310,"y":480,"wires":[["d4d479e614e82a49","7eb760e019b512dc"]]},{"id":"a82c03c3d34f683c","type":"delay","z":"bdd7be38.d3b55","name":"Some more stuff to do","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":800,"y":480,"wires":[["7c6253e5d34769ac","b23cea1074943d4d"]]},{"id":"2128a855234c1016","type":"link in","z":"bdd7be38.d3b55","name":"link in 1","links":["7c6253e5d34769ac"],"x":95,"y":560,"wires":[["3a9faf0a95b4a9bb"]]},{"id":"7c6253e5d34769ac","type":"link out","z":"bdd7be38.d3b55","name":"link out 1","mode":"link","links":["2128a855234c1016"],"x":665,"y":560,"wires":[]},{"id":"b23cea1074943d4d","type":"debug","z":"bdd7be38.d3b55","name":"OUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":400,"wires":[]},{"id":"d4d479e614e82a49","type":"debug","z":"bdd7be38.d3b55","name":"IN","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":400,"wires":[]},{"id":"3a9faf0a95b4a9bb","type":"function","z":"bdd7be38.d3b55","name":"Flush","func":"return {flush: 1}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":190,"y":560,"wires":[["ed63ee4225312b40"]]},{"id":"7eb760e019b512dc","type":"function","z":"bdd7be38.d3b55","name":"Some functions to be performed","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":480,"wires":[["a82c03c3d34f683c"]]},{"id":"e35f37deeae94860","type":"comment","z":"bdd7be38.d3b55","name":"Set the queue timeout to larger than you ever expect the process to take","info":"This is a simple flow which allows a sequence of nodes to be \nprotected so that only one message is allowed in at a time. \nIt uses a Delay node in Rate Limit mode to queue them, but \nreleases them, using the Flush mechanism, as soon as the \nprevious one is complete. Set the timeout in the delay node to \na value greater than the maximum time you expect it ever to take. \nIf for some reason the flow locks up (a message fails to indicate \ncompletion) then the next message will be released after that time.\n\nMake sure that you trap any errors and feed back to the Flush \nnode when you have handled the error. Also make sure only one \nmessage is fed back for each one in, even in the case of errors.","x":270,"y":360,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 June 2024 08:53 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/9 "2024-06-04T08:53:51Z")

</div>

The German translation came up before but I do not think an issue was raised [Queue-gate (q-gate) question - #11 by dceejay](https://discourse.nodered.org/t/queue-gate-q-gate-question/86576/11)

---

<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:** [4 June 2024 13:21 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/10 "2024-06-04T13:21:47Z")

</div>

@kitori, If the translation is not correct, please submit an issue at [Issues · node-red/node-red · GitHub](https://github.com/node-red/node-red/issues)

---

<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:** [3 August 2024 13:21 UTC](https://discourse.nodered.org/t/on-demand-delay-node/88466/11 "2024-08-03T13:21:55Z")

</div>

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