WebSockets 00:00:01 Open a two-way room Two friends open a live group chat. In this HTTP/1.1 example a browser begins with a secure connection and asks the server to upgrade it to WebSocket. Both directions can then carry messages. The browser connects to port 443, as for any page. A GET asks to upgrade, with a random key and the Origin of the page. The service validates the origin, authenticates the session as required and checks capacity before accepting the connection. The server accepts the handshake with status one hundred and one. The connection can now carry WebSocket frames. Either side may speak first. Here the server does. The initial handshake establishes a transport channel. Joining a room and being authorized to read its history are application-level operations with their own checks. 00:00:49 A phone vanishes without saying goodbye A tunnel can silence a phone without delivering a close frame. A server-side connection record can therefore outlive the device's actual reachability. Every 30 seconds the server asks. The browser answers by itself. Page code sees neither. The phone is gone. No close frame was sent, so the server still sees an open connection. Ten seconds pass. The server closes and removes the subscriptions. The memory is free. Heartbeats trade traffic for detection time. Choose intervals and deadlines that account for mobile networks and background restrictions, and reclaim resources when the policy declares the session gone. 00:01:26 Reconnect after message forty-two A connection does not replay history by itself. The application assigns stable sequence positions to room messages and retains a history it can query after reconnect. Numbers 41 and 42 arrive. The connection breaks. 43 and 44 are saved meanwhile. Between 0 and 1 second for the first try, longer for later ones. The new connection tells the server that forty-two was the last position it processed. 43 and 44 from the store, then 45 live. Handle the boundary between replay and live delivery carefully. Dedupe by message identity and define what happens if the retained history no longer goes back far enough. 00:02:06 Two friends land on different servers One friend uses server one, the other server two. An in-memory list of local sockets can reach only the users physically connected to that process. Server 1 holds A and gets the message. The store gives it number 42. Server 1 publishes to room 7. Server 2 is subscribed to room 7, because it holds B. Server 2 writes to B. Use a shared event distribution mechanism for live notifications and an appropriate durable store for replay. Losing a socket-serving machine should cost a reconnection, not the room's history. 00:02:40 A slow phone needs a bounded buffer Now apply the same connection to a live quote display. The newest quote for a symbol may supersede an older unsent one, unlike a chat message that must remain in order. Prices reach both phones. The slow phone stops reading. Its prices wait. A new NQ price replaces the old one. Two entries, however long the tunnel is. With 1,000 symbols the limit is reached. The server closes this client only. Choose a buffer policy from the meaning of the message. Coalesce replaceable state, preserve important events through replay and bound memory per slow client rather than risking the entire server.