Skip to main content
Version: 4.x

Redis 流适配器

工作原理

¥How it works

适配器将使用 Redis 流 在 Socket.IO 服务器之间转发数据包。

¥The adapter will use a Redis stream to forward packets between the Socket.IO servers.

与现有 Redis 适配器(使用 Redis Pub/Sub 机制)的主要区别在于,该适配器将正确处理与 Redis 服务器的任何临时断开连接并恢复流,而不会丢失任何数据包。

¥The main difference with the existing Redis adapter (which use the Redis Pub/Sub mechanism) is that this adapter will properly handle any temporary disconnection to the Redis server and resume the stream without losing any packets.

注意:

¥Notes:

  • 单个流用于所有命名空间

    ¥a single stream is used for all namespaces

  • maxLen 选项允许限制流的大小

    ¥the maxLen option allows to limit the size of the stream

  • 与基于 Redis PUB/SUB 机制的适配器不同,该适配器将正确处理与 Redis 服务器的任何临时断开连接并恢复流

    ¥unlike the adapter based on Redis PUB/SUB mechanism, this adapter will properly handle any temporary disconnection to the Redis server and resume the stream

  • 如果启用 连接状态恢复,会话将作为经典键/值对存储在 Redis 中

    ¥if connection state recovery is enabled, the sessions will be stored in Redis as a classic key/value pair

源代码:https://github.com/socketio/socket.io-redis-streams-adapter

¥Source code: https://github.com/socketio/socket.io-redis-streams-adapter

支持的功能

¥Supported features

特性socket.io 版本支持
套接字管理4.0.0:白色复选标记:是(自版本 0.1.0 起)
服务器间通信4.1.0:白色复选标记:是(自版本 0.1.0 起)
广播并致谢4.5.0:白色复选标记:是(自版本 0.1.0 起)
连接状态恢复4.6.0:白色复选标记:是(自版本 0.1.0 起)

安装

¥Installation

npm install @socket.io/redis-streams-adapter redis

用法

¥Usage

使用 redis

¥With the redis package

import { createClient } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-streams-adapter";

const redisClient = createClient({ url: "redis://localhost:6379" });

await redisClient.connect();

const io = new Server({
adapter: createAdapter(redisClient)
});

io.listen(3000);

使用 redis 包和 Redis 集群

¥With the redis package and a Redis cluster

import { createCluster } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-streams-adapter";

const redisClient = createCluster({
rootNodes: [
{
url: "redis://localhost:7000",
},
{
url: "redis://localhost:7001",
},
{
url: "redis://localhost:7002",
},
],
});

await redisClient.connect();

const io = new Server({
adapter: createAdapter(redisClient)
});

io.listen(3000);

使用 ioredis

¥With the ioredis package

import { Redis } from "ioredis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-streams-adapter";

const redisClient = new Redis();

const io = new Server({
adapter: createAdapter(redisClient)
});

io.listen(3000);

使用 ioredis 包和 Redis 集群

¥With the ioredis package and a Redis cluster

import { Cluster } from "ioredis";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-streams-adapter";

const redisClient = new Cluster([
{
host: "localhost",
port: 7000,
},
{
host: "localhost",
port: 7001,
},
{
host: "localhost",
port: 7002,
},
]);

const io = new Server({
adapter: createAdapter(redisClient)
});

io.listen(3000);

选项

¥Options

名称描述默认值
streamNameRedis 流的名称。socket.io
maxLen流的最大大小。使用几乎精确的修剪 (~)。10_000
readCount每个 XREAD 调用要获取的元素数量。100
sessionKeyPrefix当启用连接状态恢复功能时,用于存储 Socket.IO 会话的密钥的前缀。sio:session:
heartbeatInterval两次心跳之间的毫秒数。5_000
heartbeatTimeout在我们考虑节点关闭之前没有心跳的毫秒数。10_000

常见问题

¥Common questions

使用 Redis Streams 适配器时是否仍然需要启用粘性会话?

¥Do I still need to enable sticky sessions when using the Redis Streams adapter?

是的。如果不这样做,将导致 HTTP 400 响应(你正在访问不知道 Socket.IO 会话的服务器)。

¥Yes. Failing to do so will result in HTTP 400 responses (you are reaching a server that is not aware of the Socket.IO session).

更多信息可参见 此处

¥More information can be found here.

当 Redis 服务器宕机时会发生什么?

¥What happens when the Redis server is down?

与经典的 Redis 适配器 不同,该适配器将正确处理与 Redis 服务器的任何临时断开连接并恢复流,而不会丢失任何数据包。

¥Unlike the classic Redis adapter, this adapter will properly handle any temporary disconnection to the Redis server and resume the stream without losing any packets.

最新版本

¥Latest releases

版本发布日期发行说明差异
0.2.12024 年 3 月link0.2.0...0.2.1
0.2.02024 年 2 月link0.1.0...0.2.0
0.1.02023 年 4 月link

完整的变更日志

¥Complete changelog