WebSocket
WebSocket is a protocol that establishes a persistent, full-duplex communication channel between a client and server over a single TCP connection.
§ 1 Definition
WebSocket is the answer to a fundamental limitation of HTTP: HTTP is request-response. The client always initiates. The server can never push data without being asked. WebSocket reverses this. After an initial HTTP upgrade handshake, the connection stays open. Both client and server can send messages at any time. This makes WebSocket essential for real-time applications: chat apps, live notifications, collaborative editing, financial tickers, multiplayer games, and streaming dashboards. WebSocket uses a frame-based message protocol rather than HTTP's request-response model. It supports text and binary frames. The WebSocket API in the browser exposes a simple `send()` method and `onmessage` event. On the server side, libraries like `ws` (Node.js) or framework-native WebSocket support handle the protocol.
§ 2 How it works
A WebSocket connection starts with an HTTP upgrade request. The client sends `GET` with headers `Upgrade: websocket` and `Connection: Upgrade`. The server responds with `101 Switching Protocols`. The connection is then upgraded from HTTP to the WebSocket protocol. Both sides can now send frames independently. The connection stays open until one side closes it. If the connection drops, the client must re-establish it (there is no automatic reconnect in the spec).
§ 3 WebSocket vs. alternatives
Server-Sent Events (SSE) is a simpler one-directional alternative: server pushes to client only. For unidirectional use cases (notifications, news feeds), SSE is easier and works over standard HTTP. For bidirectional communication, WebSocket is the standard. Long polling (repeated HTTP requests simulating push) is a hack that wastes bandwidth and server resources. If you are long polling in 2026, you should probably switch to WebSocket.
§ 4 Common misconception
WebSocket does not replace HTTP. It complements it. The initial handshake is HTTP. WebSocket is a different protocol that operates after the upgrade. WebSocket connections also do not benefit from HTTP caching, HTTP/2 multiplexing, or HTTP/3 optimizations. You need to handle concerns like authentication, reconnection, and message ordering yourself.
§ 5 Note
§ 6 In code
```javascript
// Client-side WebSocket
const ws = new WebSocket('wss://example.com/socket');
ws.onopen = () => ws.send('Hello server!');
ws.onmessage = (event) => console.log('Received:', event.data);
ws.onclose = () => console.log('Disconnected');
```§ 7 Common questions
- Q. Is WebSocket faster than HTTP?
- A. It depends. WebSocket eliminates HTTP request/response overhead for ongoing communication. For single requests, HTTP/2 may be faster.
- Q. Can WebSocket connect to any port?
- A. Yes, but browser security policies may restrict connections to certain ports. Standard WebSocket uses ports 80 (ws) and 443 (wss).
- WebSocket enables full-duplex, persistent communication between client and server.
- It starts as an HTTP upgrade then switches to a frame-based protocol.
- Essential for real-time applications like chat, live updates, and collaboration.
Atomic Glue builds real-time features using WebSocket, from live dashboards to collaborative tools. Our Website Development services include full-stack real-time architecture.
Get in touchWebSocket is a protocol that establishes a persistent, full-duplex communication channel between a client and server over a single TCP connection.
Category: Web Development (also: Apis, Architecture)
Author: Atomic Glue Team
## Definition
WebSocket is the answer to a fundamental limitation of HTTP: HTTP is request-response. The client always initiates. The server can never push data without being asked. WebSocket reverses this. After an initial HTTP upgrade handshake, the connection stays open. Both client and server can send messages at any time. This makes WebSocket essential for real-time applications: chat apps, live notifications, collaborative editing, financial tickers, multiplayer games, and streaming dashboards. WebSocket uses a frame-based message protocol rather than HTTP's request-response model. It supports text and binary frames. The WebSocket API in the browser exposes a simple `send()` method and `onmessage` event. On the server side, libraries like `ws` (Node.js) or framework-native WebSocket support handle the protocol.
## How it works
A WebSocket connection starts with an HTTP upgrade request. The client sends `GET` with headers `Upgrade: websocket` and `Connection: Upgrade`. The server responds with `101 Switching Protocols`. The connection is then upgraded from HTTP to the WebSocket protocol. Both sides can now send frames independently. The connection stays open until one side closes it. If the connection drops, the client must re-establish it (there is no automatic reconnect in the spec).
## WebSocket vs. alternatives
Server-Sent Events (SSE) is a simpler one-directional alternative: server pushes to client only. For unidirectional use cases (notifications, news feeds), SSE is easier and works over standard HTTP. For bidirectional communication, WebSocket is the standard. Long polling (repeated HTTP requests simulating push) is a hack that wastes bandwidth and server resources. If you are long polling in 2026, you should probably switch to WebSocket.
## Common misconception
WebSocket does not replace HTTP. It complements it. The initial handshake is HTTP. WebSocket is a different protocol that operates after the upgrade. WebSocket connections also do not benefit from HTTP caching, HTTP/2 multiplexing, or HTTP/3 optimizations. You need to handle concerns like authentication, reconnection, and message ordering yourself.
## Note
Use `wss://` (WebSocket Secure) for encrypted connections, just like you use `https://`. Plain `ws://` is unencrypted and will be blocked by many browsers on secure pages.
## In code
## Common questions Q: Is WebSocket faster than HTTP? A: It depends. WebSocket eliminates HTTP request/response overhead for ongoing communication. For single requests, HTTP/2 may be faster. Q: Can WebSocket connect to any port? A: Yes, but browser security policies may restrict connections to certain ports. Standard WebSocket uses ports 80 (ws) and 443 (wss).
## Key takeaways
- WebSocket enables full-duplex, persistent communication between client and server.
- It starts as an HTTP upgrade then switches to a frame-based protocol.
- Essential for real-time applications like chat, live updates, and collaboration.
## Related entries
- [HTTP](atomicglue.co/glossary/http)
- [API](atomicglue.co/glossary/api)
- [JavaScript](atomicglue.co/glossary/javascript)
- [HTTP/2](atomicglue.co/glossary/http2)
Last updated June 2026. Permalink: atomicglue.co/glossary/websocket