WebSocket Room

noun

say it: “WEB-sah-kit ROOM” /ˈwɛbˌsɑkɪt rum/

A named group of live connections that receive the same messages, such as everyone following one game.

What is a room for WebSockets?

A room is a named group of WebSocket clients that get the same messages, such as everyone following one game. The server keeps a list of connections for each room and sends a room's messages to that list only.

const rooms = new Map(); // "game-42" -> a Set of sockets

function sendToRoom(room, message) {
  for (const socket of rooms.get(room) ?? []) socket.send(message);
}
sendToRoom("game-42", "Goal for the home team!");

In a sentence

  • “Fans of game 42 join the game-42 WebSocket room, so they don't get scores from other games.”
  • “Closing the page takes the connection out of its WebSocket room.”

Level: Journeyman · More Web Development terms at this level

Updated