توسيع نطاق الإشارة باستخدام الغرف وRedis
تعلّم توسيع نطاق خادم الإشارة في WebRTC أفقيًا باستخدام توجيه الرسائل القائم على الغرف ومحوّل Redis pub/sub، ليتمكن النظراء على مثيلات خوادم مختلفة من الاتصال أيضًا.
توسيع نطاق الإشارة باستخدام الغرف وRedis درس مجاني في Real-Time Streaming Systems (WebRTC + Live Data) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.
الأسئلة الشائعة
هل درس «توسيع نطاق الإشارة باستخدام الغرف وRedis» مجاني؟
نعم — نص درس «توسيع نطاق الإشارة باستخدام الغرف وRedis» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اختيار الواجهة الخلفية للإشارة
- تنفيذ منطق الإشارة
- نشر الإشارة واختبارها
- توسيع نطاق الإشارة باستخدام الغرف وRedis