Scalabilità del signaling con room e Redis
Impari a scalare orizzontalmente un server di signaling WebRTC usando il routing dei messaggi basato sulle room e un adapter Redis pub/sub, così i peer su istanze server diverse possono comunque connettersi.
Scalabilità del signaling con room e Redis è una lezione Real-Time Streaming Systems (WebRTC + Live Data) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Real-Time Streaming Systems (WebRTC + Live Data), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Real-Time Streaming Systems (WebRTC + Live Data) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Real-Time Streaming Systems (WebRTC + Live Data) con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Scalabilità del signaling con room e Redis» è gratuita?
Sì — il testo completo di «Scalabilità del signaling con room e Redis» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Real-Time Streaming Systems (WebRTC + Live Data), passa a CoddyKit PRO. Il corso Real-Time Streaming Systems (WebRTC + Live Data) include 4 lezioni in totale.
Cosa imparerò in «Scalabilità del signaling con room e Redis»?
Impari a scalare orizzontalmente un server di signaling WebRTC usando il routing dei messaggi basato sulle room e un adapter Redis pub/sub, così i peer su istanze server diverse possono comunque conn… Eserciti Real-Time Streaming Systems (WebRTC + Live Data) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Real-Time Streaming Systems (WebRTC + Live Data)?
Non è richiesta alcuna esperienza precedente. Real-Time Streaming Systems (WebRTC + Live Data) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Scalabilità del signaling con room e Redis»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Real-Time Streaming Systems (WebRTC + Live Data)?
Sì. Ogni lezione Real-Time Streaming Systems (WebRTC + Live Data) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Scelta del backend per la segnalazione
- Implementazione della logica di segnalazione
- Distribuzione e test della segnalazione
- Scalabilità del signaling con room e Redis