WebSocket
WebSocket ProtocolBest for: Chat, live scores and games
- Upgrade, from You
- 101 OK, from Chat server
- Hi, from You
- New message, from Chat server
- New message, from Chat server
Compare protocols
Best for: Chat, live scores and games
Best for: Pages, files and simple APIs
HTTP is request and response: the browser asks, the server answers, and the server cannot speak first. WebSocket starts as an HTTP request, upgrades the same TCP connection, then keeps it open so both sides can send small messages at any time. Use WebSocket for chat, live scores and multiplayer. Use HTTP for pages, files and APIs.
Tick everything that is true for you.
Your pick shows here
Tick one or more lines and we weigh them for you.
| What differs | WebSocket | HTTP |
|---|---|---|
| Who can speak first | Both sides, any time | Only the client |
| Connection | One long-lived TCP connection | Reused with keep-alive, but each exchange is a new request |
| Start | HTTP Upgrade, then 101 Switching Protocols | Directly with a request |
| Overhead per message | 2 to 14 bytes of frame header | Often hundreds of bytes of headers |
| Caching | No | Yes, by browsers and proxies |
| Addresses | ws:// (port 80) and wss:// (port 443) | http:// (80) and https:// (443) |
| Scaling servers | Harder: each user holds a connection | Easier: requests are independent |
| Standard | RFC 6455 | RFC 9110 (semantics) |
Each page plays the real packets on a map, step by step, with every header explained.
Each line describes one of the two. Which one?
Question 1 of 8
Who can speak first
Both sides, any time
Only to start. The browser sends an HTTP GET with an Upgrade: websocket header. After the server replies 101 Switching Protocols, the connection speaks WebSocket frames, not HTTP.
For many small messages, yes, because there is no new request and no big headers each time. For one large download, plain HTTP is just as fast.
The same as the web: 80 for ws:// and 443 for wss://. That is why it passes most firewalls.
Server-Sent Events let the server push to the browser over normal HTTP, one way only. They are simpler when the client never needs to send messages back on the same line.