إدارة الغرف والقنوات
نظّم عملاء WebSocket في غرف وقنوات بحيث تصل الرسائل إلى المجموعة المعنية فقط، وهو الأساس لتطبيقات الدردشة والتعاون.
إدارة الغرف والقنوات درس مجاني في WebSockets & Realtime Systems Programming على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في WebSockets & Realtime Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة WebSockets & Realtime Systems Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Rooms
Broadcasting to everyone does not scale for multi-topic apps. Rooms (or channels) let you group clients so a message only reaches members of one group, like a single chat room.
Modeling Rooms
A simple model maps a room name to a set of client connections.
const rooms = new Map(); // roomName -> Set<ws>Joining a Room
When a client asks to join, add its connection to that room set, creating the set on first use.
function join(room, ws) {
if (!rooms.has(room)) rooms.set(room, new Set());
rooms.get(room).add(ws);
}Leaving a Room
Remove the connection on leave, and clean up empty rooms to avoid leaks.
function leave(room, ws) {
const set = rooms.get(room);
if (!set) return;
set.delete(ws);
if (set.size === 0) rooms.delete(room);
}A Join Message Protocol
Clients signal intent with structured messages. Parse the type and route accordingly.
ws.on('message', (raw) => {
const msg = JSON.parse(raw);
if (msg.type === 'join') join(msg.room, ws);
});Broadcasting to a Room
To send to a room, iterate only that room set, skipping closed sockets.
function toRoom(room, payload) {
const set = rooms.get(room);
if (!set) return;
for (const client of set) {
if (client.readyState === 1) client.send(payload);
}
}Tracking a Client\u2019s Rooms
Store room membership on the connection so cleanup on disconnect is easy.
ws.rooms = new Set();Cleaning Up on Disconnect
When a socket closes, remove it from every room it joined.
ws.on('close', () => {
for (const room of ws.rooms) leave(room, ws);
});Channels vs Rooms
Terms vary. Often a channel is a named topic clients subscribe to, while a room is a private group. Mechanically both are just keyed sets of subscribers.
Presence Awareness
You can derive presence from room membership: the size of a room set is the number of online members, and join/leave events can notify others.
toRoom(room, JSON.stringify({ type: 'presence', count: rooms.get(room).size }));Best Practices
Keep room management robust:
- Always clean up on disconnect
- Skip sockets not in OPEN state
- Validate room names from clients
- Remove empty rooms to free memory
Quick Check
Test your rooms knowledge.
Recap
You added rooms to your WebSocket server:
- Map room names to sets of connections
- Handle join/leave via structured messages
- Broadcast only to a room\u2019s members
- Clean up on disconnect and derive presence
This is the backbone of chat and collaboration features.
الأسئلة الشائعة
هل درس «إدارة الغرف والقنوات» مجاني؟
نعم — نص درس «إدارة الغرف والقنوات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebSockets & Realtime Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة WebSockets & Realtime Systems Programming 4 دروس في المجموع.
ماذا ستتعلم في «إدارة الغرف والقنوات»؟
نظّم عملاء WebSocket في غرف وقنوات بحيث تصل الرسائل إلى المجموعة المعنية فقط، وهو الأساس لتطبيقات الدردشة والتعاون. تتمرن على WebSockets & Realtime Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ WebSockets & Realtime Systems Programming؟
لا تُشترط خبرة سابقة. WebSockets & Realtime Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «إدارة الغرف والقنوات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس WebSockets & Realtime Systems Programming هذا؟
نعم. كل درس في WebSockets & Realtime Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد مشروع Node.js
- تنفيذ المنطق من جهة الخادم
- بث الرسائل إلى العملاء
- إدارة الغرف والقنوات