การจัดการห้องและช่องทาง
จัดกลุ่มไคลเอนต์ WebSocket เป็นห้องและช่องทาง เพื่อให้ข้อความไปถึงเฉพาะกลุ่มที่เกี่ยวข้อง ซึ่งเป็นรากฐานของแอปแชตและแอปทำงานร่วมกัน
การจัดการห้องและช่องทาง เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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.
คำถามที่พบบ่อย
บทเรียน “การจัดการห้องและช่องทาง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการห้องและช่องทาง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Realtime Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการห้องและช่องทาง”
จัดกลุ่มไคลเอนต์ WebSocket เป็นห้องและช่องทาง เพื่อให้ข้อความไปถึงเฉพาะกลุ่มที่เกี่ยวข้อง ซึ่งเป็นรากฐานของแอปแชตและแอปทำงานร่วมกัน คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Realtime Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Realtime Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการห้องและช่องทาง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม
ได้ บทเรียน WebSockets & Realtime Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่าโครงการ Node.js
- การใช้งานตรรกะฝั่งเซิร์ฟเวอร์
- การกระจายข้อความไปยังไคลเอนต์
- การจัดการห้องและช่องทาง