WebSocket is a communication protocol that provides full-duplex, persistent connections between clients and servers over a single TCP connection. Unlike HTTP, where the client must initiate every request, WebSocket allows both parties to send messages at any time without the overhead of opening new connections. HTTP is request-response: client asks, server answers, connection closes. This model is efficient for page loads and API calls but wasteful for real-time applications that need continuous data streams. WebSocket solves this by upgrading an HTTP connection to a persistent bidirectional channel. The use cases are real-time by nature: live chat, collaborative editing, financial tickers, multiplayer games, live sports scores. Any application that needs to push updates to clients without polling benefits from WebSocket. The alternative (polling, where clients repeatedly ask 'anything new?') wastes bandwidth and adds latency proportional to poll frequency. WebSocket delivers updates instantly when they occur. WebSocket connections are stateful, which complicates horizontal scaling. Traditional load balancers route each request independently, but WebSocket connections must reach the same server or use a pub/sub backplane. Sticky sessions or message brokers like Redis solve this.
Back to Glossary