


We use cookies to improve your experience
We use essential cookies to make our site work. With your consent, we may also use non-essential cookies to improve user experience.
Definition
WebSocket is a communication protocol that provides full-duplex, persistent connections between a client and server over a single TCP connection. Unlike HTTP's request-response model, WebSockets allow both sides to send data at any time, enabling real-time features.
HTTP is request-response: the client asks, the server answers, the connection effectively resets. For real-time applications — chat, live scores, stock tickers, collaborative editing — this model is inefficient. Polling (repeatedly asking "any new data?") wastes bandwidth and adds latency. WebSockets solve this by establishing a single, persistent connection where either side can send messages instantly without the overhead of new HTTP requests.
A WebSocket connection starts as a regular HTTP request with an "Upgrade" header. If the server agrees, the connection is "upgraded" from HTTP to the WebSocket protocol (ws:// or wss:// for encrypted). From that point, both client and server can send frames of data at any time. The connection stays open until one side explicitly closes it or a network failure occurs. Messages are lightweight — just 2-6 bytes of framing overhead compared to HTTP headers that can be hundreds of bytes.
Chat applications, multiplayer games, live dashboards, collaborative document editing (Google Docs), real-time notifications, and financial trading platforms all rely on WebSockets. Libraries like Socket.IO (JavaScript) and SignalR (.NET) add features like automatic reconnection and fallback to polling for older environments. For most real-time web features, WebSockets are the standard solution.