WebSocket is a protocol that lets a client and server exchange messages freely in both directions over a single persistent connection.
Escaping the request-response limits of HTTP, the server can push data first — ideal for real-time apps like chat, live collaboration, games, and live quotes.
The connection starts as HTTP: the client requests an upgrade with the `Upgrade: websocket` header, the server replies `101 Switching Protocols`, and from that moment the same TCP connection leaves HTTP and becomes full-duplex WebSocket frame messaging. Afterward it doesn't repeat headers per message, so overhead is low and low-latency real-time messaging is possible. URL schemes are `ws://` (plaintext) and `wss://` (TLS).
Comparing with SSE is a common decision point: SSE is a one-way (server→client) text stream over plain HTTP, making implementation, proxies, and reconnection simple. WebSocket is bidirectional and handles binary data, but as its own protocol it makes infrastructure, auth, reconnection, and state management more complex. So for one-way cases like server notifications or live feeds, SSE fits; for chat or collaborative editing where the client also sends actively, WebSocket fits. Operationally, load balancers and proxies must support long-lived connections and upgrades, and heartbeats (ping/pong) detect dead connections.