Rate Limiter

Whilst the delay module has a function to limit rates, it buffers the unsent msg's and dispatches them in sequence according to the limit rate set.
This code (courtesy of Nick O'Leary) limits the msg flow similar to above, except it 'throws away' all of the intermediate msg's, instead of queuing them. A further advantage is that you can set the limit rate to any value (within reason!) instead of being constrained to 1 sec,1 min, 1 hr.

The limit rate is set in the first line of code (in milliseconds).


[{"id":"3a82b835.c57d48","type":"function","name":"Rate Limiter","func":"var interval = (1000*60*10); // 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":327,"y":756,"z":"3f1992f2.c0e66e","wires":[[]]}]

Flow Info

created 4 years, 5 months ago

Node Types

  • function (x1)


  • limiter
  • rate
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option