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.

  1. Browser
  2. Server
  1. Upgrade requestBrowser to Server
  2. 101 Switching ProtocolsServer to Browser
  3. Frame: "Hello"Browser to Server
  4. Frame: "Hello" backServer to Browser
  5. 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

FieldTextWhat it means
Request lineGET /chat HTTP/1.1A normal HTTP request for the /chat address.
Host headerHost: example.comThe site.
Upgrade headerUpgrade: websocket"Please switch this connection to the WebSocket protocol."
Connection headerConnection: UpgradeMarks the Upgrade header as meant for this connection.
Sec-WebSocket-KeySec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==A random value. The server must prove it understands WebSocket by transforming it.
Sec-WebSocket-VersionSec-WebSocket-Version: 13The WebSocket version, which is 13.
Blank lineEnds 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=

FieldTextWhat it means
Status lineHTTP/1.1 101 Switching Protocols101 means "agreed: from now on this is not HTTP".
Upgrade headerUpgrade: websocketConfirms the new protocol.
Connection headerConnection: UpgradeConfirms the switch.
Sec-WebSocket-AcceptSec-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 lineEnds 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

FieldBytesWhat it means
FIN and opcode81Binary 1000 0001: FIN means this is the last piece of the message, opcode 1 means text.
Mask flag and length85Binary 1000 0101: the first bit says the data is masked (browsers must mask), and the length is 5.
Masking key37 fa 21 3dFour random bytes. Each data byte is combined with one of them (XOR).
Masked data7f 9f 4d 51 58These 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

FieldBytesWhat it means
FIN and opcode81Same as before: a final text frame.
Mask flag and length05The mask flag is 0 because servers do not mask. The length is 5.
DataHelloThe 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.

Back to Network Lab

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.