ws-rate-limit
Rate limiting utility for
ws
This rate limits the 'message'
event on ws
objects. Also triggers an additional 'limited'
event for handling.
// Initialize module at 100 requests per 10 seconds:var rateLimit = 100 '10s' // Handle connections on WS serverwss
This can also be used with uws
because they are compatible.
Installation
npm install --save ws-rate-limit
Usage
rateLimit(rate, max) -> limiter
Creates a limiter
function with your specified rate limiting options. Used to easily reapply same settings on multiple connections.
Parameters
rate
(String
): Acss-duration
string of the rate. (i.e.10m
,0.5d
,1w
, etc.)max
(Number
): Maximum amount of requests that can be made during the rate.
Example
// 100 requests per 10 seconds:var limiter = // 5000 requests every half a day:var limiter =
limiter(client)
Apply rate limiting options on a ws
client.
Note: This will unset your 'message'
event handlers in order to create a new rate limited one wrapping it. Try using 'newListener'
event to cache for unsetting yourself.
Parameters
client
(WebSocket
): Aws
websocket client to apply rate limits.
Example
var limiter = wss
'limited'
Event Event is triggered instead of 'message'
when the rate limiting has capped in the time frame.
Example
wss
License
MIT © Jamen Marz