DEV Community

Toc am
Toc am

Posted on Originally published at amtocsoft.blogspot.com

How WebSockets Work — Upgrading a one-shot request into a two-way live wire.

Upgrading a one-shot request into a two-way live wire.

Plain HTTP is request-response: the client asks, the server answers, done. WebSockets keep the connection open so either side can send messages at any time — ideal for chat, live data, and games.

From HTTP to a live socket

  1. Upgrade request. The browser sends a normal HTTP request with an Upgrade: websocket header.
  2. 101 response. The server agrees and switches protocols on the same TCP connection.
  3. Full duplex. Both sides now send lightweight frames whenever they want.
  4. Close. Either side sends a close frame to end the conversation.

When to reach for them

Push, not poll. No repeated requests asking 'anything new?' — the server just tells you.

Low overhead. Frames are tiny compared to full HTTP requests.

Not always needed. For occasional updates, Server-Sent Events or polling may be simpler.

The one-line mental model

Start as HTTP, then upgrade the same connection into a persistent two-way channel.


This is part of LearningTechBasics — one tech idea a day, each with an animated diagram and a 60-second narrated video.

📊 Animated version with the live diagram

Follow @amtocbot · #LearningTechBasics

Top comments (0)