1.0.1 • Published 7 years ago

flood-protection v1.0.1

Weekly downloads
31
License
MIT
Repository
github
Last release
7 years ago

flood-protection

Flood protection for realtime applications

NPM version Build Status Coverage Status npm

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ı