Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

ratelimiter

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ratelimiter

abstract rate limiter backed by redis


Version published
Weekly downloads
408K
decreased by-20.51%
Maintainers
2
Weekly downloads
 
Created

What is ratelimiter?

The 'ratelimiter' npm package is a simple and efficient tool for rate limiting in Node.js applications. It helps control the rate at which requests are processed, preventing abuse and ensuring fair usage of resources. It is particularly useful in scenarios where you need to limit the number of requests a user can make to an API within a certain timeframe.

What are ratelimiter's main functionalities?

Basic Rate Limiting

This code demonstrates basic rate limiting using the 'ratelimiter' package. It sets a limit of 5 requests per minute for a user with ID 'user123'. The remaining requests are logged to the console.

const RateLimiter = require('ratelimiter');
const redis = require('redis');
const client = redis.createClient();

const limit = new RateLimiter({
  id: 'user123',
  db: client,
  max: 5, // max requests
  duration: 60000 // per minute
});

limit.get((err, limit) => {
  if (err) throw err;
  console.log(limit.remaining); // remaining requests
});

Custom Rate Limiting

This code demonstrates custom rate limiting where a user with ID 'user456' is allowed 10 requests per 30 seconds. It checks if the user has remaining requests and logs whether the request is allowed or if the rate limit is exceeded.

const RateLimiter = require('ratelimiter');
const redis = require('redis');
const client = redis.createClient();

const limit = new RateLimiter({
  id: 'user456',
  db: client,
  max: 10, // max requests
  duration: 30000 // per 30 seconds
});

limit.get((err, limit) => {
  if (err) throw err;
  if (limit.remaining) {
    console.log('Request allowed');
  } else {
    console.log('Rate limit exceeded');
  }
});

Other packages similar to ratelimiter

Keywords

FAQs

Package last updated on 24 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc