ルームとRedisによるシグナリングのスケーリング
ルームベースのメッセージルーティングとRedis pub/subアダプターを使ってWebRTCシグナリングサーバーを水平スケールし、異なるサーバーインスタンス上のピア同士も接続できるようにします。
「ルームとRedisによるシグナリングのスケーリング」はCoddyKit上の無料Real-Time Streaming Systems (WebRTC + Live Data)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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によるシグナリングのスケーリング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Real-Time Streaming Systems (WebRTC + Live Data)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Real-Time Streaming Systems (WebRTC + Live Data)コースには全4レッスンが含まれています。
「ルームとRedisによるシグナリングのスケーリング」で何を学びますか?
ルームベースのメッセージルーティングとRedis pub/subアダプターを使ってWebRTCシグナリングサーバーを水平スケールし、異なるサーバーインスタンス上のピア同士も接続できるようにします。 ブラウザで直接実行するハンズオンコードでReal-Time Streaming Systems (WebRTC + Live Data)を演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- シグナリング用バックエンドの選択
- シグナリング処理の実装
- シグナリングのデプロイとテスト
- ルームとRedisによるシグナリングのスケーリング