TodayTools

WebSocket Protocol

How WebSocket works

A two-way line that stays open

Donate
L7ApplicationPort 443TCP
What brings you here?

Watch it happen

How much detail

Opening a team chat: one HTTP upgrade, then messages flow both ways on the same connection.

Lucas's laptop
Home router
Internet
chat.example.com
Wi-FiInternetSetupRequestResponseDataControlEncrypted

3 steps, 6 packets

First up: Upgrade request. Press Play to watch it happen, one step at a time.

0 of 30/3

What is WebSocket?

WebSocket gives a web page a connection that stays open, where both the browser and the server can send a message at any moment. It starts as an ordinary HTTP request that asks to upgrade, and after a 101 reply the same TCP connection carries small WebSocket frames instead of HTTP.

Plain HTTP only answers when asked, so live apps used to ask over and over: anything new yet? WebSocket lets the server push the moment something happens. Chat apps, live scores, shared documents, trading screens and multiplayer games use it.

Think of it like this. HTTP is sending letters: you write, wait, and get one reply per letter. WebSocket is a phone call: once connected, either person can talk whenever they like, and the line stays open until someone hangs up. Ping and pong are the little are you still there, yes I am of a quiet call.

Step by step

Normal

3 steps

Opening a team chat: one HTTP upgrade, then messages flow both ways on the same connection.

  1. 1

    Upgrade request

    HTTP asks to switch

    The page runs new WebSocket('wss://chat.example.com/chat'). The TCP and TLS connection is set up as for any HTTPS page, then the browser sends a GET with Upgrade: websocket.

  2. 2

    Lucas sends

    A small masked text frame

    Lucas types a message. It goes as one WebSocket frame with a header of just 6 bytes, instead of a whole new HTTP request with hundreds of bytes of headers.

  3. 3

    Server pushes

    Messages arrive without asking

    When Mateo replies, the server sends it straight down the open connection. The browser did not have to ask, which plain HTTP cannot do.

Ping and close

3 steps

Keeping a quiet connection alive with ping and pong, then closing it politely.

  1. 1

    Ping

    Are you still there?

    Nobody has typed for a while. Routers and proxies drop connections that look idle, so the server sends a small ping frame every 30 seconds or so.

  2. 2

    Pong

    Yes, I am here

    The browser answers every ping with a pong by itself. Web pages cannot even see these frames.

  3. 3

    Close

    Close frame with code 1000

    Lucas closes the tab. The browser sends a close frame with status 1000 (normal closure). The server echoes it, then TCP is closed.

Line drops

3 steps

Lucas's Wi-Fi cuts out for a few seconds. Nobody sends a close frame, so the app must notice and reconnect.

  1. 1

    Silent drop

    Messages vanish

    The Wi-Fi goes down. Frames sent now are lost, and neither side is told. A dead TCP connection can look open for minutes.

  2. 2

    Ping goes unanswered

    No pong comes back

    The server's ping gets no pong. After a timeout it gives up on the connection. The page sees a close event with code 1006, meaning closed without a close frame.

  3. 3

    Reconnect

    New connection, catch up

    Good apps reconnect with a short, growing delay and ask for messages since the last one they saw, so nothing is missed.

Myths and mix-ups

  • WebSocket is a separate network with its own port; it starts as HTTP on the same ports 80 and 443.
  • WebSocket messages always arrive even after a drop; the protocol has no catch-up, the app must add it.
  • Masking the browser's frames is encryption; it is only a guard against confused proxies, wss:// adds the real encryption.
  • Every live feature needs WebSocket; one-way updates often work fine with Server-Sent Events over plain HTTP.

Try it yourself

Run these on your own computer to see WebSocket at work.

  • See the upgrade in the browser

    F12, Network tab, filter Socket (or WS), click the row, then Messages

    Shows the 101 response and every frame sent and received, with times.

  • Open a socket from the console

    const s = new WebSocket('wss://example.com/socket'); s.onmessage = e => console.log(e.data);

    Replace the address with a WebSocket server you own or one that offers an echo test.

  • Check the upgrade with curl

    curl -i -N -H 'Connection: Upgrade' -H 'Upgrade: websocket' -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' https://your-server.example/socket

    A WebSocket server answers 101 Switching Protocols. Newer curl versions also accept wss:// addresses directly.

WebSocket at a glance

Layer
Application (7)
Port
443 for wss://, 80 for ws://
Transport
TCP
Starts with
HTTP/1.1 Upgrade, 101 reply
Frame header
2 to 14 bytes
Message types
Text, binary, ping, pong, close
Published
2011

Defined inRFC 6455RFC 8441RFC 9220

Header fields

What WebSocket puts in its messages, with example values from the stories above.

WebSocket

FIN
1This frame is a whole message. Long messages can be split across frames with FIN 0.
Opcode
0x1 (text)
Mask
1Browsers must scramble their frames with a random key, so old proxies cannot be fooled into caching attacker data.
Payload length
46
Masking key
0x3f8a21c7
Direction
Server to browserThe server starts this one. With plain HTTP the browser would have to keep asking: anything new?
Payload
4 bytesAny small payload. The pong must echo it back.
Status code
1000 (Normal Closure)1001 means going away, 1008 policy violation, 1011 a server error. 1006 is never sent; it means the line dropped.

Test yourself

Question 1 of 5Score 0

How does a WebSocket connection start?

Questions people ask

What is the difference between WebSocket and HTTP?

HTTP is one request, one response, and only the browser asks. WebSocket starts as HTTP, then keeps one connection open where both sides send messages whenever they want.

What port does WebSocket use?

The same as the web: 443 for secure wss:// and 80 for ws://. That is why it works through most firewalls.

Is WebSocket secure?

Use wss://, which runs inside TLS just like HTTPS. Plain ws:// is not encrypted. The server should also check the Origin header and log-in tokens.

Why does my WebSocket keep disconnecting?

Proxies, load balancers and phone networks close connections that look idle. Send pings every 20 to 30 seconds and reconnect with a growing delay when it drops.

What is the difference between WebSocket and Server-Sent Events?

Server-Sent Events only send from server to browser over normal HTTP and reconnect on their own. WebSocket is two-way and supports binary data.

Donate