Lab · Foundations

WebSockets & heartbeats

A phone opens one connection that stays open, and messages travel both ways on it. Pick how the connection ends, and how often the gateway checks on it, then step forward and back.

Try it

How the connection ends

The phone goes into a tunnel and sends nothing.

Heartbeats from the gateway

With these settings: Found after 15 s. Heartbeats every 10 s found the dead connection at second 30. Before that, 1 message went into it and was lost.

one open connectionPhone, user 42Phoneuser 42asking to upgradeWebSocket gateway, gateway 1, NOWWebSocket gatewaygateway 1NOWSession registry, who is whereSession registrywho is whereChat service, sends messagesChat servicesends messages
  • Phone — user 42 — healthy — asking to upgrade
  • WebSocket gateway — gateway 1 — healthy — NOW
  • Session registry — who is where — healthy
  • Chat service — sends messages — healthy
  • Request
  • Answer
  • Record
  • Message
  • Look up
  • Ping
  • Pong
  • Lost
  • Remove
Step 1 of 17

Step 1 of 17 · WebSocket gateway

Done: The phone asks to upgrade to a WebSocket

What happens
The chat app sends an ordinary HTTP request with the header Upgrade: websocket.
Why
Starting as HTTP lets the connection use the same ports and proxies as web pages.
What changes
The gateway supports WebSockets, so it can agree.
Next
The gateway answers 101 Switching Protocols

Event log

16 more to go. Press a line to go back to that step.

Clock
0s
Connection
not open
Registry says user 42 is on
nobody
Heartbeats missed
0 of 2
Messages lost
0

Step 1 of 17: WebSocket gateway. The phone asks to upgrade to a WebSocket. The chat app sends an ordinary HTTP request with the header Upgrade: websocket.

What this model simplifies
  • This is one phone, one gateway and times in whole seconds. A real gateway holds many thousands of connections, and each uses memory until it is closed.
  • Here the gateway sends the pings. In many apps the phone sends them, which works the same way.
  • Real apps usually number their messages, so a phone that reconnects can ask for the ones it missed.