WebSockets & Realtime Systems Programming · 课时

使用 Redis 构建发布/订阅后端

使用 Redis 发布/订阅后端连接多个 WebSocket 服务器实例,让消息无论客户端连接到哪个节点都能送达。

第 4 / 4 课13 个步骤

使用 Redis 构建发布/订阅后端 是 CoddyKit 上的免费 WebSockets & Realtime Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebSockets & Realtime Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Multi-Instance Problem

When you scale horizontally, clients connect to different server instances. A message published on node A will not reach a client connected to node B unless the nodes share it.

What a Backplane Does

A backplane is a shared channel every instance subscribes to. When one node receives a message, it publishes to the backplane, and all nodes deliver it to their local clients.

Why Redis Pub/Sub

Redis pub/sub is fast, simple, and widely available. Publishers send to a channel; all subscribers receive it instantly, making it a natural backplane.

Two Redis Connections

Redis requires separate clients for publishing and subscribing, because a subscribed connection cannot run normal commands.

const pub = redis.createClient();
const sub = redis.createClient();
await pub.connect();
await sub.connect();

Subscribing on Startup

Each instance subscribes to the shared channel when it boots.

await sub.subscribe('ws-broadcast', (message) => {
  deliverLocally(JSON.parse(message));
});

Publishing Across Nodes

Instead of broadcasting only to local sockets, publish to Redis so every node hears it.

function broadcast(payload) {
  pub.publish('ws-broadcast', JSON.stringify(payload));
}

Delivering Locally

The subscribe callback fans the message out to the clients connected to that specific instance.

function deliverLocally(payload) {
  for (const client of localClients) {
    if (client.readyState === 1) client.send(JSON.stringify(payload));
  }
}

Avoiding Echo Loops

Since the publishing node also receives its own message via subscribe, deliver only through the backplane path so each message is sent once, not twice.

Scoping by Room

For room-based apps, include the room in the payload and let each node deliver only to its local members of that room, keeping the backplane channel count small.

pub.publish('ws-broadcast', JSON.stringify({ room: 'chat-1', data }));

Limits of Redis Pub/Sub

Redis pub/sub is fire-and-forget: offline subscribers miss messages, and there is no persistence. For durability or replay, consider Redis Streams or a dedicated broker.

Best Practices

Run a healthy backplane:

  • Use separate pub and sub clients
  • Route all broadcasts through the backplane
  • Scope messages by room to reduce work
  • Know that pub/sub does not persist messages

Quick Check

Test your backplane knowledge.

Recap

You wired up a Redis pub/sub backplane:

  • Use separate publish and subscribe clients
  • Publish broadcasts to a shared channel
  • Each node delivers to its local clients
  • Scope by room and accept fire-and-forget limits

Your WebSocket app now scales across many instances.

免费开始

用 AI 导师学习 WebSockets & Realtime Systems Programming — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
47

常见问题解答

「使用 Redis 构建发布/订阅后端」课时是免费的吗?

是的 — 「使用 Redis 构建发布/订阅后端」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebSockets & Realtime Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。

「使用 Redis 构建发布/订阅后端」这节课中我会学到什么?

使用 Redis 发布/订阅后端连接多个 WebSocket 服务器实例,让消息无论客户端连接到哪个节点都能送达。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Realtime Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebSockets & Realtime Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebSockets & Realtime Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 Redis 构建发布/订阅后端」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebSockets & Realtime Systems Programming 课中编写并运行代码吗?

能。每节 WebSockets & Realtime Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 横向扩展策略
  2. WebSockets 负载均衡
  3. 分布式状态管理
  4. 使用 Redis 构建发布/订阅后端
← 返回 WebSockets & Realtime Systems Programming