使用房间与 Redis 扩展信令
学习使用基于房间的消息路由和 Redis 发布/订阅适配器,水平扩展 WebRTC 信令服务器,让不同服务器实例上的对等端仍能连接。
使用房间与 Redis 扩展信令 是 CoddyKit 上的免费 Real-Time Streaming Systems (WebRTC + Live Data) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Real-Time Streaming Systems (WebRTC + Live Data) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Real-Time Streaming Systems (WebRTC + Live Data) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
One Server Is Not Enough
You have built, deployed, and tested a signaling server. As users grow, a single instance becomes a bottleneck. This lesson covers scaling signaling horizontally across multiple instances using rooms and Redis.
The Room Concept
Signaling messages should only reach the right peers. A room groups the participants of one call so offers, answers, and ICE candidates are routed only to members of that room.
Joining a Room
When a client connects, it joins a room identified by a call id. The server tracks which sockets belong to which room.
io.on('connection', (socket) => {
socket.on('join', (roomId) => {
socket.join(roomId);
socket.to(roomId).emit('peer-joined', socket.id);
});
});Routing Within a Room
Signaling messages are relayed only to other members of the sender's room, never broadcast to everyone.
socket.on('signal', ({ roomId, data }) => {
socket.to(roomId).emit('signal', { from: socket.id, data });
});The Multi-Instance Problem
With several server instances behind a load balancer, two peers in the same call may connect to different instances. Instance A does not know about a room member on instance B, so signaling fails.
Pub/Sub to the Rescue
A shared Redis pub/sub layer lets instances forward messages to each other. When instance A emits to a room, Redis publishes it so instance B delivers it to its local members.
Adding the Redis Adapter
Socket.IO offers a Redis adapter that handles cross-instance routing transparently, so your room code stays unchanged.
const { createAdapter } = require('@socket.io/redis-adapter');
const { createClient } = require('redis');
const pub = createClient({ url: 'redis://localhost:6379' });
const sub = pub.duplicate();
await Promise.all([pub.connect(), sub.connect()]);
io.adapter(createAdapter(pub, sub));Sticky Sessions
For long-lived WebSocket connections, configure the load balancer for sticky sessions so a client stays on one instance for the life of its connection, avoiding handshake breakage.
Tracking Presence
Store room membership in Redis so any instance can answer who is in a call and clean up when a client disconnects.
socket.on('join', async (roomId) => {
await pub.sAdd('room:' + roomId, socket.id);
});
socket.on('disconnect', async () => {
// remove from all rooms it belonged to
});Handling Disconnects
Notify remaining peers when someone leaves so they can tear down the corresponding peer connection cleanly.
socket.on('disconnect', () => {
socket.rooms.forEach((roomId) => {
socket.to(roomId).emit('peer-left', socket.id);
});
});Scaling Strategy Summary
To scale signaling: group peers into rooms, run multiple stateless instances, connect them with a Redis adapter, enable sticky sessions, and track presence in Redis. The signaling layer then grows horizontally while calls keep connecting.
Quick Check
Test your understanding of scaling signaling.
Recap
You learned to scale signaling:
- Rooms route messages only to call participants
- Multiple instances need a Redis pub/sub adapter to share rooms
- Sticky sessions keep WebSocket connections stable
- Presence tracking and disconnect handling keep state consistent
This architecture supports many concurrent calls reliably.
常见问题解答
「使用房间与 Redis 扩展信令」课时是免费的吗?
是的 — 「使用房间与 Redis 扩展信令」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Real-Time Streaming Systems (WebRTC + Live Data) 课程的其余内容,请升级到 CoddyKit PRO。 Real-Time Streaming Systems (WebRTC + Live Data) 课程共包含 4 节课。
「使用房间与 Redis 扩展信令」这节课中我会学到什么?
学习使用基于房间的消息路由和 Redis 发布/订阅适配器,水平扩展 WebRTC 信令服务器,让不同服务器实例上的对等端仍能连接。 你通过在浏览器中直接运行的动手代码来练习 Real-Time Streaming Systems (WebRTC + Live Data),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Real-Time Streaming Systems (WebRTC + Live Data) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Real-Time Streaming Systems (WebRTC + Live Data) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用房间与 Redis 扩展信令」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Real-Time Streaming Systems (WebRTC + Live Data) 课中编写并运行代码吗?
能。每节 Real-Time Streaming Systems (WebRTC + Live Data) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。