Why it is hard
A WebSocket is a single TCP connection kept open for the life of a session, carrying messages in both directions. Unlike HTTP requests that finish quickly, these connections persist, so a server must hold state for every client at once.
The connection lifecycle
- A client sends an HTTP upgrade request that switches the protocol to WebSocket.
- The server registers the connection in a table keyed by user or session.
- Messages flow until either side closes or a heartbeat declares the link dead.
What bounds a node
- File descriptors are consumed one per connection, so the operating system limit caps capacity.
- Memory per connection for buffers and registration adds up across tens of thousands of clients.
- The event loop must stay non blocking so one slow client cannot stall others.
Operational practice
- Track open connections as a metric so you can scale before limits are hit.
- Drain connections gracefully on deploy by telling clients to reconnect elsewhere.
Key idea
WebSocket connection management means holding persistent per client state in a registry while respecting the file descriptor, memory, and event loop limits that bound each node.