WebSocket
WebSocket is a protocol that provides a persistent, bidirectional connection between client and server. Unlike HTTP, both sides can send data at any time without starting a new request.
In a world where users expect real-time updates – whether chat, live prices or multiplayer games – the classic HTTP request-response model reaches its limits. WebSockets solve this by opening a persistent connection between browser and server over which both sides can send data at any time. The result: real-time communication with minimal latency and without the overhead of repeated HTTP requests.
What is WebSocket?
WebSocket is a network protocol (RFC 6455) that provides a full-duplex (bidirectional), persistent connection over a single TCP connection. The connection starts with an HTTP upgrade handshake where client and server switch from HTTP to the WebSocket protocol (ws:// or wss:// for encrypted). After that the connection stays open and both sides can send messages as text or binary frames. WebSockets are stateful (unlike HTTP): the connection remains until explicitly closed. The protocol has very low overhead (2–14 bytes per frame header), which makes it ideal for high-frequency data.
How does WebSocket work?
The client sends a normal HTTP request with a special Upgrade header. If the server accepts, it responds with HTTP 101 (Switching Protocols) and the connection switches to WebSocket. From then on the TCP connection is open and both sides can send independently. Heartbeats (Ping/Pong frames) monitor the connection and detect drops. Clients typically implement automatic reconnection with exponential backoff.
Practical Examples
A chat app uses WebSockets to send messages in real time to all participants in a room without users refreshing.
A stock ticker sends price updates via WebSocket in milliseconds to thousands of connected browsers.
A collaborative whiteboard syncs drawings, text and cursor positions in real time between all participants.
A monitoring dashboard receives server metrics and log entries via WebSocket and shows them in real time in charts.
A multiplayer browser game uses WebSockets to sync player positions, actions and game state with minimal latency.
Typical Use Cases
Chat and messaging that need immediate delivery
Live dashboards and monitoring that show metrics and events in real time
Collaborative editors (documents, code, design) where multiple users work at once
Financial apps with real-time quotes, order books and trade feeds
IoT applications that stream sensor data from devices to a web interface
Advantages and Disadvantages
Advantages
- True bidirectional: The server can push data to clients without waiting for a request
- Low latency: Data is sent as soon as it is ready, without new connection overhead
- Efficiency: Small protocol overhead (few bytes per frame) saves bandwidth for high-frequency messages
- Persistent connection: One open connection avoids repeated TCP and TLS handshakes
- Wide support: All modern browsers and server frameworks support WebSockets
Disadvantages
- Connection management: Persistent connections consume server resources – scaling to many clients is non-trivial
- Complexity: Reconnection, heartbeats and state sync need more implementation work than REST
- Proxy and firewall issues: Some networks block or terminate WebSocket connections
- No built-in request-response: For classic CRUD, REST is often simpler
Frequently Asked Questions about WebSocket
When should I use WebSockets instead of REST?
How do I scale WebSocket applications?
Are WebSockets secure?
Related Terms
Want to use WebSocket in your project?
We are happy to advise you on WebSocket and find the optimal solution for your requirements. Benefit from our experience across over 200 projects.