TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About Β· Labs Β· Cheat sheets Β· Learning paths

← Back to WebSocket

πŸ“ž WebSocket: cheat sheet

🎨 The analogySwapping letters for a phone call: keep the line open, and either side can talk the moment something happens.
πŸŽ“ The regular explanationA single long-lived, two-way connection between browser and server. It starts as an HTTP request, upgrades with 101 Switching Protocols, and then carries small frames in both directions at any time.
Asking again and againStep 3 of 3
WebSocketπŸ§’YouπŸ§‘Your friend
  1. With letters, you can't hear news until you ask. So you keep writing, "anything new?"
  2. Most answers are "no". That's a lot of letters, and news still waits until your next one.
  3. A phone call is different. Once it's connected, the line stays open and either of you can speak whenever you like.
Turning a letter into a phone callStep 4 of 4
πŸ§’YouπŸ§‘Your friend
  1. The call starts as an ordinary letter, with a note on the envelope, "can we switch to a phone call?"
  2. Your friend answers 101, "switching to the phone", and the same line turns into a call.
  3. Now you just talk. Short sentences go both ways with no envelopes or forms each time.
  4. When you're done, one of you says goodbye, the other says goodbye back, and you both hang up.
A group callStep 4 of 4
πŸ‘§AnaπŸ‘¦Benβœ• βœ•πŸ’«Cy☎️Switchboard2 in room
  1. Three friends each keep a line open to the switchboard.
  2. Ana says something. The switchboard passes it straight on to Ben and Cy.
  3. Every so often the switchboard asks "still there?" and each friend says "yep", so dead lines are noticed.
  4. Cy goes into a tunnel and the line drops. The switchboard stops sending to Cy, and Cy's phone calls back when it can.

🧩 Key questions

Why is following a chat by letter tiring?
You keep writing "anything new?", and nearly every answer is "no". On a phone call, either side can speak the moment there's news.
How does the phone call start?
With an ordinary letter asking "can we switch to the phone?". The same line turns into a call.
A friend's line drops. What happens?
The switchboard notices on its next check, and the friend's phone calls back by itself. The switchboard checks now and then that each line is still alive.