What are WebSockets and when to use them
· Category: Networking
Short answer
WebSockets provide a full-duplex, persistent connection between a client and server over a single TCP connection. They enable real-time, low-latency communication without repeatedly opening new HTTP requests.
How it works
A WebSocket connection begins with an HTTP handshake. The client sends an upgrade request, and the server responds with a 101 Switching Protocols status. After the handshake, the connection remains open, allowing both sides to send messages at any time.
This eliminates the overhead of HTTP polling and reduces latency for applications requiring instant updates.
Example
Live chat applications, stock tickers, online gaming, and collaborative document editors all use WebSockets to push updates to clients immediately when data changes.
Why it matters
WebSockets transform the request-response model into a true conversational flow. They reduce server load compared to polling, improve responsiveness, and enable scalable real-time features.