Web and appsApplicationRFC 6455
WebSocket: a two-way line
A connection that stays open so the server can push messages to the browser at any time.
Ports and use: TCP 80 or 443 (starts as HTTP)
In one sentenceA WebSocket begins as an HTTP request, switches protocols, and then both sides can send small frames whenever they like.
Think of it like thisHTTP is sending letters back and forth. WebSocket is a phone call that stays connected.
Watch it happen
5 steps and 4 byte-by-byte decodes. Use the buttons or the arrow keys. Hover or focus a field to find its bytes.
- Browser
- Server
- Upgrade requestBrowser to Server
- 101 Switching ProtocolsServer to Browser
- Frame: "Hello"Browser to Server
- Frame: "Hello" backServer to Browser
- CloseBrowser to Server
Step 1 of 5 Upgrade request
Browser → Server
The browser asks to switch this HTTP connection to WebSocket.
HTTP upgrade request 154 bytes
GET /chat HTTP/1.1↵ Host: example.com↵ Upgrade: websocket↵ Connection: Upgrade↵ Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==↵ Sec-WebSocket-Version: 13↵ ↵
| Field | Text | What it means |
|---|---|---|
| Request line | GET /chat HTTP/1.1↵ | A normal HTTP request for the /chat address. |
| Host header | Host: example.com↵ | The site. |
| Upgrade header | Upgrade: websocket↵ | "Please switch this connection to the WebSocket protocol." |
| Connection header | Connection: Upgrade↵ | Marks the Upgrade header as meant for this connection. |
| Sec-WebSocket-Key | Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==↵ | A random value. The server must prove it understands WebSocket by transforming it. |
| Sec-WebSocket-Version | Sec-WebSocket-Version: 13↵ | The WebSocket version, which is 13. |
| Blank line | ↵ | Ends the headers. |
Step 2 of 5 101 Switching Protocols
Server → Browser
The server agrees. The reply proves it understood the request, then HTTP is over for this connection.
HTTP upgrade response 129 bytes
HTTP/1.1 101 Switching Protocols↵ Upgrade: websocket↵ Connection: Upgrade↵ Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=↵ ↵
| Field | Text | What it means |
|---|---|---|
| Status line | HTTP/1.1 101 Switching Protocols↵ | 101 means "agreed: from now on this is not HTTP". |
| Upgrade header | Upgrade: websocket↵ | Confirms the new protocol. |
| Connection header | Connection: Upgrade↵ | Confirms the switch. |
| Sec-WebSocket-Accept | Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=↵ | The key joined with a fixed text, hashed with SHA-1 and encoded. It proves the server really speaks WebSocket. |
| Blank line | ↵ | Ends the headers. The connection stays open. |
Step 3 of 5 Frame: "Hello"
Browser → Server
The browser sends a frame with a 2-byte header. Frames from a browser are masked to protect shared caches and proxies from confusion.
Client frame, masked 11 bytes
81 85 37 fa 21 3d 7f 9f 4d 51 58
| Field | Bytes | What it means |
|---|---|---|
| FIN and opcode | 81 | Binary 1000 0001: FIN means this is the last piece of the message, opcode 1 means text. |
| Mask flag and length | 85 | Binary 1000 0101: the first bit says the data is masked (browsers must mask), and the length is 5. |
| Masking key | 37 fa 21 3d | Four random bytes. Each data byte is combined with one of them (XOR). |
| Masked data | 7f 9f 4d 51 58 | These bytes decode to "Hello" once each is XORed with the masking key in turn. |
Step 4 of 5 Frame: "Hello" back
Server → Browser
The server can answer or push a message at any time. Its frame is not masked.
Server frame 7 bytes
81 05 Hello
| Field | Bytes | What it means |
|---|---|---|
| FIN and opcode | 81 | Same as before: a final text frame. |
| Mask flag and length | 05 | The mask flag is 0 because servers do not mask. The length is 5. |
| Data | Hello | The text, sent as it is. |
Step 5 of 5 Close
Browser → Server
Either side sends a close frame with a status code, such as 1000 for a normal closure, and the connection ends.
Where you meet it
- Chat apps, live comments and multiplayer games.
- Live prices, scores and dashboards that update without refreshing.
- Collaborative editors where many people change one document.
Common mistakes
- Using WebSocket when plain polling or server-sent events would do. They are simpler to run.
- Forgetting to reconnect after the connection drops on a phone.
- Not sending pings, so proxies and load balancers close the idle connection.
- Not checking who is connecting: WebSocket does not follow the browser's usual same-origin rules, so check the Origin header.
Try it yourself
Run these on your own computer and compare the output with the lesson above.
npx wscat -c wss://echo.websocket.eventsA command-line client; type a message and see it come back.In the browser console: new WebSocket("wss://echo.websocket.events")Open the Network tab to see the 101 response and frames.
Addresses and values in the examples are made up for teaching (the 203.0.113.x range is reserved for documentation). Real traffic carries more options and details. Check the standard named above for the full rules.