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

@socket.io/redis-adapter

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@socket.io/redis-adapter

The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers

  • 8.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
206K
increased by6.49%
Maintainers
1
Weekly downloads
 
Created

What is @socket.io/redis-adapter?

@socket.io/redis-adapter is an adapter for Socket.IO that allows you to use Redis as a message broker to enable horizontal scaling. It facilitates communication between multiple Socket.IO servers by using Redis Pub/Sub mechanism.

What are @socket.io/redis-adapter's main functionalities?

Basic Setup

This code demonstrates the basic setup of the @socket.io/redis-adapter. It initializes a Socket.IO server and configures it to use Redis for message brokering.

const { createAdapter } = require('@socket.io/redis-adapter');
const { Server } = require('socket.io');
const { createClient } = require('redis');

const io = new Server();
const pubClient = createClient({ host: 'localhost', port: 6379 });
const subClient = pubClient.duplicate();

io.adapter(createAdapter(pubClient, subClient));
io.listen(3000);

Broadcasting Events

This code shows how to broadcast events to all connected clients using the @socket.io/redis-adapter. When a 'message' event is received, it is broadcasted to all clients.

io.on('connection', (socket) => {
  socket.on('message', (data) => {
    io.emit('message', data);
  });
});

Handling Rooms

This code demonstrates how to handle rooms with the @socket.io/redis-adapter. Clients can join rooms and messages can be sent to specific rooms.

io.on('connection', (socket) => {
  socket.on('joinRoom', (room) => {
    socket.join(room);
  });

  socket.on('message', (data) => {
    io.to(data.room).emit('message', data.message);
  });
});

Other packages similar to @socket.io/redis-adapter

FAQs

Package last updated on 13 Mar 2024

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