Delay function

1,301 views
Skip to first unread message

Steven Keller

unread,
Feb 24, 2014, 8:56:57 PM2/24/14
to node...@googlegroups.com
I have a UDP packet that is broadcast to a particular port about every second.  Node-Red has no problem receiving the packet.  I would however only like to process a packet every 5 or 10 seconds.  (maybe longer depending on the application.)   Using the Delay function doesn't seem to work in the scenario unless I am missing something.  If I delay on time it just buffers the packets for a particular period of time and them forwards them on to the next node.  If I rate delay it still seems to buffer them and only sends them on at the rate requested.  I assume at some point I will run out of memory using the rate delay.

Is there some other way to handle this?


Dave C-J

unread,
Feb 25, 2014, 4:10:05 AM2/25/14
to node...@googlegroups.com
Hi Steven
so what do you want to do with the other packets ? drop them ? average over them ? something else ?
Do they arrive every second  - so you could just pick every 5 or 10th one to process ? or do you want to take the next one to arrive after a 10 second gap ?

sorry for the questions - but a bit more clarity is required. A simple function node would be the way to do it... whatever it is :-)


On 25 February 2014 01:56, Steven Keller <skelle...@gmail.com> wrote:
I have a UDP packet that is broadcast to a particular port about every second.  Node-Red has no problem receiving the packet.  I would however only like to process a packet every 5 or 10 seconds.  (maybe longer depending on the application.)   Using the Delay function doesn't seem to work in the scenario unless I am missing something.  If I delay on time it just buffers the packets for a particular period of time and them forwards them on to the next node.  If I rate delay it still seems to buffer them and only sends them on at the rate requested.  I assume at some point I will run out of memory using the rate delay.

Is there some other way to handle this?


--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
regards

Dave Conway-Jones

Nicholas O'Leary

unread,
Feb 25, 2014, 6:17:44 AM2/25/14
to node...@googlegroups.com
Steven,

as Dave says, there are lots of things that could be done, but assuming you want to simply drop packets that arrive within the interval, here is a function node that does that:

[{"id":"6206b1a.f9df95","type":"function","name":"Rate limit","func":"var interval = 5000; // minimum interval between messages (ms)\ncontext.lastTime = context.lastTime || 0;\n\nvar now = Date.now();\n\nif (now-context.lastTime > interval) {\n  context.lastTime = now;\n  return msg;\n} else {\n  return null;\n}","outputs":1,"x":474,"y":498,"z":"ab8bcf4.f54743","wires":[["c5bf0002.3a41"]]}]

Currently set to allow a packet through every 5 seconds - easily changed in the first line of code.

Nick

Steven Keller

unread,
Feb 25, 2014, 9:43:11 PM2/25/14
to node...@googlegroups.com
Sorry for the lack of details.  For now I simply needed to drop the packets.  Nick your solution works perfect for now.  As I get more advanced I will need to track which IP address I have received data from.  Each IP Address will need to have its own lastTime variable.  I will cross that bridge when I get to it.

Thanks for the help.  I am starting to learn my way around node.js/javascript/node-red enough to do something productive.  I have managed to create a custom UDP receive/send node to communicate with my device and send the data to ThingSpeak.  So far so good.

Thanks
steve




You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/lI5NlY1oesc/unsubscribe.
To unsubscribe from this group and all of its topics, send an email to node-red+u...@googlegroups.com.
Message has been deleted

Gladclef

unread,
Aug 2, 2017, 11:23:52 AM8/2/17
to Node-RED
I suggest a different take on this node:

[{"id":"7a58e436.7066cc","
type":"inject","z":"a3ab65bf.43184","name":"1/2 sec delay","topic":"set delay","payload":"500","payloadType":"num","repeat":"","crontab":"","once":true,"x":109,"y":544,"wires":[["b422c1eb.666988","14c4a2bb.a32ccd","ab57c97c.5f6098"]]},{"id":"ab57c97c.5f6098","type":"function","z":"a3ab65bf.43184","name":"rate limit","func":"var delay = 500; // milliseconds between messages\n\n// check for outside updates to the delay\nif (msg.topic === 'set delay')\n{\n    context.set('outsideDelay', msg.payload);\n    return;\n}\nif (typeof context.get('outsideDelay') !== 'undefined')\n{\n    delay = context.get('outsideDelay');\n}\n\n// check for immediate updates\nvar now = Date.now();\nvar prev = now;\nif (typeof context.get('prev') !== 'undefined')\n{\n    prev = context.get('prev');\n}\nvar nextDelayPoint = delay - (now - prev);\ndelay = Math.max(nextDelayPoint, 0);\n\n// clear the existing timeout\nvar timeout = null;\nif (typeof context.get('timeout') !== 'undefined')\n{\n    timeout = context.get('timeout');\n    clearTimeout(timeout);\n}\n\n// set a new timeout\ntimeout = setTimeout(function() {\n    node.send(msg);\n}, delay);\ncontext.set('timeout', timeout);\ncontext.set('prev', now);","outputs":1,"noerr":0,"x":300,"y":547,"wires":[[]]}]

Features:
* rate limit to 1 msg every delay milliseconds
* the latest msg is passed through, always
* immediate pass through if its been over delay milliseconds
* delay milliseconds can be managed from another node via a "set timeout" msg topic (allowing easy delay modification of many rate limit nodes)

Bart Butenaers

unread,
Aug 2, 2017, 5:30:03 PM8/2/17
to Node-RED
Hi Steven,

I often use the node-red-contrib-throttle node. E.g. it has an "on time" option: if you set it to 5 seconds, it will pass only one message during every 5 seconds.

I assume that could also have solved your issue?

Kind regards,
Bart Butenaers

Dave C-J

unread,
Aug 2, 2017, 5:46:35 PM8/2/17
to node...@googlegroups.com
The delay node also has a mode (timed queue I think), that will release the most recent values across a range of topics at set intervals
--
Sent from phone.
Reply all
Reply to author
Forward
0 new messages