Real-Time Streaming Systems (WebRTC + Live Data) · บทเรียน

การขยายระบบส่งสัญญาณด้วยห้องและ Redis

เรียนรู้การขยายเซิร์ฟเวอร์ส่งสัญญาณ WebRTC ในแนวนอนด้วยการกำหนดเส้นทางข้อความตามห้องและตัวปรับ Redis pub/sub เพื่อให้เพียร์บนอินสแตนซ์เซิร์ฟเวอร์ต่างกันยังเชื่อมต่อกันได้

บทเรียน 4 จาก 413 ขั้นตอน

การขยายระบบส่งสัญญาณด้วยห้องและ Redis เป็นบทเรียน Real-Time Streaming Systems (WebRTC + Live Data) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

เริ่มต้นได้ฟรี

เรียนรู้ Real-Time Streaming Systems (WebRTC + Live Data) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การขยายระบบส่งสัญญาณด้วยห้องและ Redis” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การขยายระบบส่งสัญญาณด้วยห้องและ Redis” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Real-Time Streaming Systems (WebRTC + Live Data) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Real-Time Streaming Systems (WebRTC + Live Data) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การขยายระบบส่งสัญญาณด้วยห้องและ Redis”

เรียนรู้การขยายเซิร์ฟเวอร์ส่งสัญญาณ WebRTC ในแนวนอนด้วยการกำหนดเส้นทางข้อความตามห้องและตัวปรับ Redis pub/sub เพื่อให้เพียร์บนอินสแตนซ์เซิร์ฟเวอร์ต่างกันยังเชื่อมต่อกันได้ คุณปฏิบัติ Real-Time Streaming Systems (WebRTC + Live Data) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Real-Time Streaming Systems (WebRTC + Live Data) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Real-Time Streaming Systems (WebRTC + Live Data) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การขยายระบบส่งสัญญาณด้วยห้องและ Redis” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Real-Time Streaming Systems (WebRTC + Live Data) นี้ได้ไหม

ได้ บทเรียน Real-Time Streaming Systems (WebRTC + Live Data) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเลือกแบ็กเอนด์สำหรับการส่งสัญญาณ
  2. การนำตรรกะการส่งสัญญาณไปใช้งาน
  3. การติดตั้งใช้งานและทดสอบระบบส่งสัญญาณ
  4. การขยายระบบส่งสัญญาณด้วยห้องและ Redis
← กลับไปที่ Real-Time Streaming Systems (WebRTC + Live Data)