Skip to main content
Version: 4.x

Postgres 适配器

工作原理

¥How it works

Postgres 适配器依赖于 NOTIFYLISTEN 命令。

¥The Postgres adapter relies on the NOTIFY and LISTEN commands.

发送到多个客户端(例如 io.to("room1").emit()socket.broadcast.emit())的每个数据包是:

¥Every packet that is sent to multiple clients (e.g. io.to("room1").emit() or socket.broadcast.emit()) is:

  • 发送到连接到当前服务器的所有匹配客户端

    ¥sent to all matching clients connected to the current server

  • 如果数据包包含二进制数据或超过 8000 字节限制,则数据包为:

    ¥if the packet contains binary data or is above the 8000 bytes limit, the packet is:

    • msgpack 编码并插入辅助表中

      ¥encoded with msgpack and inserted in an auxiliary table

    • 行 ID 在 NOTIFY 命令中发送

      ¥the row ID is sent within a NOTIFY command

    • 该行 ID 由集群中的其他 Socket.IO 服务器接收,这些服务器查询表、解码数据包,然后将其广播到自己的一组连接客户端

      ¥this row ID is received by the other Socket.IO servers of the cluster, which query the table, decode the packet and then broadcast it to their own set of connected clients

  • 否则,数据包只是在 NOTIFY 命令中发送并由集群中的其他 Socket.IO 服务器接收

    ¥else, the packet is simply sent within a NOTIFY command and received by the other Socket.IO servers of the cluster

Diagram of how the Postgres adapter worksDiagram of how the Postgres adapter works

该适配器的源代码可以找到 此处

¥The source code of this adapter can be found here.

支持的功能

¥Supported features

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

安装

¥Installation

npm install @socket.io/postgres-adapter pg

对于 TypeScript 用户,你可能还需要 @types/pg

¥For TypeScript users, you might also need @types/pg.

用法

¥Usage

import { Server } from "socket.io";
import { createAdapter } from "@socket.io/postgres-adapter";
import pg from "pg";

const io = new Server();

const pool = new pg.Pool({
user: "postgres",
host: "localhost",
database: "postgres",
password: "changeit",
port: 5432,
});

pool.query(`
CREATE TABLE IF NOT EXISTS socket_io_attachments (
id bigserial UNIQUE,
created_at timestamptz DEFAULT NOW(),
payload bytea
);
`);

pool.on("error", (err) => {
console.error("Postgres error", err);
});

io.adapter(createAdapter(pool));
io.listen(3000);

选项

¥Options

名称描述默认值
uid该节点的 ID随机 ID
channelPrefix通知通道前缀socket.io
tableName超过 8000 字节限制或包含二进制数据的有效负载表的名称socket_io_attachments
payloadThreshold有效负载大小的阈值(以字节为单位)8000
requestsTimeout服务器间请求(例如带有 ack 的 fetchSockets()serverSideEmit())的超时5000
heartbeatInterval两次心跳之间的毫秒数5000
heartbeatTimeout在我们考虑节点关闭之前没有心跳的毫秒数10000
cleanupInterval两次清理查询之间的毫秒数30000

常见问题

¥Common questions

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

¥Do I still need to enable sticky sessions when using the Postgres 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.

当 Postgres 服务器关闭时会发生什么?

¥What happens when the Postgres server is down?

如果与 Postgres 服务器的连接被切断,数据包只会发送到连接到当前服务器的客户端。

¥In case the connection to the Postgres server is severed, the packets will only be sent to the clients that are connected to the current server.

最新版本

¥Latest releases

版本发布日期发行说明差异
0.4.02024 年 7 月link0.3.1...0.4.0
0.3.12023 年 2 月link0.3.0...0.3.1
0.3.02022 年 4 月link0.2.0...0.3.0
0.2.02021 年 12 月link0.1.1...0.2.0
0.1.12021 年 6 月link0.1.0...0.1.1
0.1.02021 年 6 月link

完整的变更日志

¥Complete changelog

触发器

¥Emitter

Postgres 触发器允许从另一个 Node.js 进程向连接的客户端发送数据包:

¥The Postgres emitter allows sending packets to the connected clients from another Node.js process:

Diagram of how the Postgres emitter worksDiagram of how the Postgres emitter works

安装

¥Installation

npm install @socket.io/postgres-emitter pg

用法

¥Usage

const { Emitter } = require("@socket.io/postgres-emitter");
const { Pool } = require("pg");

const pool = new Pool({
user: "postgres",
host: "localhost",
database: "postgres",
password: "changeit",
port: 5432,
});

const emitter = new Emitter(pool);

setInterval(() => {
emitter.emit("ping", new Date());
}, 1000);

请参阅备忘单 此处

¥Please refer to the cheatsheet here.

最新版本

¥Latest releases

版本发布日期发行说明差异
0.1.02021 年 6 月link

完整的变更日志

¥Complete changelog