Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Adapter to enable broadcasting of events to multiple separate socket.io server nodes.

License

NotificationsYou must be signed in to change notification settings

socketio/socket.io-redis-adapter

Repository files navigation

The@socket.io/redis-adapter package allows broadcasting packets between multiple Socket.IO servers.

Diagram of Socket.IO packets forwarded through Redis

Table of contents

Supported features

Featuresocket.io versionSupport
Socket management4.0.0✅ YES (since version6.1.0)
Inter-server communication4.1.0✅ YES (since version7.0.0)
Broadcast with acknowledgements4.5.0✅ YES (since version7.2.0)
Connection state recovery4.6.0❌ NO

Installation

npm install @socket.io/redis-adapter

Compatibility table

Redis Adapter versionSocket.IO server version
4.x1.x
5.x2.x
6.0.x3.x
6.1.x4.x
7.x and above4.3.1 and above

Usage

With theredis package

import{createClient}from"redis";import{Server}from"socket.io";import{createAdapter}from"@socket.io/redis-adapter";constpubClient=createClient({url:"redis://localhost:6379"});constsubClient=pubClient.duplicate();awaitPromise.all([pubClient.connect(),subClient.connect()]);constio=newServer({adapter:createAdapter(pubClient,subClient)});io.listen(3000);

With theredis package and a Redis cluster

import{createCluster}from"redis";import{Server}from"socket.io";import{createAdapter}from"@socket.io/redis-adapter";constpubClient=createCluster({rootNodes:[{url:"redis://localhost:7000",},{url:"redis://localhost:7001",},{url:"redis://localhost:7002",},],});constsubClient=pubClient.duplicate();awaitPromise.all([pubClient.connect(),subClient.connect()]);constio=newServer({adapter:createAdapter(pubClient,subClient)});io.listen(3000);

With theioredis package

import{Redis}from"ioredis";import{Server}from"socket.io";import{createAdapter}from"@socket.io/redis-adapter";constpubClient=newRedis();constsubClient=pubClient.duplicate();constio=newServer({adapter:createAdapter(pubClient,subClient)});io.listen(3000);

With theioredis package and a Redis cluster

import{Cluster}from"ioredis";import{Server}from"socket.io";import{createAdapter}from"@socket.io/redis-adapter";constpubClient=newCluster([{host:"localhost",port:7000,},{host:"localhost",port:7001,},{host:"localhost",port:7002,},]);constsubClient=pubClient.duplicate();constio=newServer({adapter:createAdapter(pubClient,subClient)});io.listen(3000);

With Redis sharded Pub/Sub

Sharded Pub/Sub was introduced in Redis 7.0 in order to help scaling the usage of Pub/Sub in cluster mode.

Reference:https://redis.io/docs/interact/pubsub/#sharded-pubsub

A dedicated adapter can be created with thecreateShardedAdapter() method:

import{Server}from"socket.io";import{createClient}from"redis";import{createShardedAdapter}from"@socket.io/redis-adapter";constpubClient=createClient({host:"localhost",port:6379});constsubClient=pubClient.duplicate();awaitPromise.all([pubClient.connect(),subClient.connect()]);constio=newServer({adapter:createShardedAdapter(pubClient,subClient)});io.listen(3000);

Minimum requirements:

Note: it is not currently possible to use the sharded adapter with theioredis package and a Redis cluster (reference).

Options

Default adapter

NameDescriptionDefault value
keyThe prefix for the Redis Pub/Sub channels.socket.io
requestsTimeoutAfter this timeout the adapter will stop waiting from responses to request.5_000
publishOnSpecificResponseChannelWhether to publish a response to the channel specific to the requesting node.false
parserThe parser to use for encoding and decoding messages sent to Redis.-

Sharded adapter

NameDescriptionDefault value
channelPrefixThe prefix for the Redis Pub/Sub channels.socket.io
subscriptionModeThe subscription mode impacts the number of Redis Pub/Sub channels used by the adapter.dynamic

License

MIT

About

Adapter to enable broadcasting of events to multiple separate socket.io server nodes.

Topics

Resources

License

Stars

Watchers

Forks

Contributors49


[8]ページ先頭

©2009-2025 Movatter.jp