flood-protection
v1.0.1
Published
Flood protection for realtime applications
Downloads
18
Maintainers
Readme
flood-protection
Flood protection for realtime applications
- This library is a direct implementation of Token Bucket Algorithm.
- I basically rewrote the python solution which was stated here for my own needs in javascript: https://stackoverflow.com/a/668327/1417536.
Install
npm install --save flood-protection
Usage
import FloodProtection from 'flood-protection';
const floodProtection = new FloodProtection({
rate: 5,
// default: 5, unit: messages
// IMPORTANT: rate must be >= 1 (greater than or equal to 1)
per: 8,
// default: 8, unit: seconds
});
Basic Example
import FloodProtection from 'flood-protection';
// ...
// io is a Socket.io instance...
io.on('connection', (client) => {
client.emit('connected');
const floodProtection = new FloodProtection();
client.on('message', (text) => {
if (floodProtection.check()) {
// forward message
io.emit('message', text);
} else {
// forbid message
client.emit('flood', {
text: 'Take it easy!',
});
}
});
});
LICENCE
MIT (c) 2017 Mehmet Yatkı