Lets say you have a site where users may open chat rooms. Once a room is opened, the users may invite others and chat.

Lets also pretend you want to use WebSockets for the communication with the clients.
In most server-side WebSocket implementations there is a simple function like connection.broadcast(message) which sends messages to all connected hosts, but not the one who initiated the broadcast. This is a great feature, because you don’t need to keep a list of all clients for your self, to keep them updated. The problem with the chat room example is that, in this case, You don’t want to broadcast every message to everyone, but only to the other clients who are in the same room.

This leaves me with two options:

  • Roll my own broadcastToRoom(room_id) function, which in turn means to track all clients and rooms.
  • Spawn a new instance of the server (on a new port) every time a new room gets created.

What would you do?