@bitsoft-network/socket.io-rate-limit

1.0.3 • Public • Published

socket.io-rate-limit

IP based token bucket rate limiting middleware for socket.io. Using @bitsoft-network/ip-token-bucket. Documentation for that package can be found here

Usage

Install the package by typing npm i @bitsoft-network/socket.io-rate-limit in your project folder.

Setup

const socketRateLimiter = require("@bitsoft-network/socket.io-rate-limit");

/* ... get access to socket.io's main object */
io.on("connection", socket => {
  // Setup rate limiting middleware
  socket.use(
    socketRateLimiter({ proxy: false, maxBurst: 5, perSecond: 1 }, socket)
  );
  /* ... your code below */
});

Methods

socketRateLimiter

Pass this with the config as an middleware to io.use().

Parameters

  • config (ConfigObject)
  • socket (SocketIO#Socket)

Returns

  • Function (SocketIO#Middleware)

Objects

ConfigObject

Object which holds all configuration values for the middleware.

Example

const config = {
  // If the socket server is running behind a proxy
  // (nginx, haproxy, etc.). This is used when getting
  // the client IP from websocket request.
  proxy: true,

  // Maximum ws event "burst" (token bucket size)
  maxBurst: 5,

  // How many tokens will be added every 1s
  perSecond: 1,
};

io.use(socketRateLimiter(config));

License

MIT <3

Package Sidebar

Install

npm i @bitsoft-network/socket.io-rate-limit

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

3.42 kB

Total Files

3

Last publish

Collaborators

  • leevihalme
  • bitsoft-admin