π 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.
- With letters, you can't hear news until you ask. So you keep writing, "anything new?"
- Most answers are "no". That's a lot of letters, and news still waits until your next one.
- A phone call is different. Once it's connected, the line stays open and either of you can speak whenever you like.
- The call starts as an ordinary letter, with a note on the envelope, "can we switch to a phone call?"
- Your friend answers 101, "switching to the phone", and the same line turns into a call.
- Now you just talk. Short sentences go both ways with no envelopes or forms each time.
- When you're done, one of you says goodbye, the other says goodbye back, and you both hang up.
- Three friends each keep a line open to the switchboard.
- Ana says something. The switchboard passes it straight on to Ben and Cy.
- Every so often the switchboard asks "still there?" and each friend says "yep", so dead lines are noticed.
- 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.