Real-Time APIs 00:00:01 Short polling spends requests on silence A chat client asks whether any messages arrived. The server replies immediately that nothing changed. If the client asks ten times more often, is the conversation ten times more useful? GET /messages answers 304: nothing changed. 304 again. A full round trip to learn nothing. 200, with a message that was written a moment after the last poll. It is almost 1 second old. One open page asks 86,400 times a day. The load grows with clients, not with messages. A validator can avoid resending an unchanged body, but the server still receives the request. A shorter interval reduces possible discovery delay at the cost of more traffic. 00:00:50 Long polling waits for evidence Ask once and let the server hold the question until it has something to say. The client asks for messages after number 41. There are none, so the server keeps the request open. Message 42 is written. The held request is answered at once. The client asks immediately, now after 42. A message written in between would be answered straight away, thanks to the cursor. After 30 seconds without news the server answers 204, before a proxy cuts the connection. The client asks again. Keep a request pending until an event arrives or a deadline expires. Reopen after the reply and handle the interval between requests using a cursor if the application promises reliable catch-up. 00:01:37 Stream events in one direction One ordinary GET whose answer never ends. The browser reconnects by itself. A normal GET that accepts text/event-stream. The answer starts and stays open. Each event is a few lines of text and a blank line. The id is remembered by the browser. A comment line about every 15 seconds keeps proxies from closing a silent connection. It cannot use this stream. It sends an ordinary POST beside it, and that is often enough. The connection breaks. The browser reconnects and sends Last-Event-ID: 42. A server that kept recent events sends 43 and 44. Server-sent events provide a server-to-client event stream over HTTP. A separate client request can still submit messages; a bidirectional application does not necessarily require both directions to use one transport. 00:02:31 Reconnect from a durable position Connections always break. Numbered messages and a saved history make it invisible. The client has shown messages up to number 42. The train enters a tunnel. Messages 43 and 44 are written and saved while nobody listens. The client waits a random time that can grow up to 30 seconds, so a million clients do not return in one second. It reconnects with after=42. 43 and 44 are sent, and then new ones as they come. The user saw a short pause and lost nothing. A connection ending does not prove which last event the client applied. Carry an event identity, retain a replay window, and tell the client when it needs a current snapshot instead. 00:03:16 Route through a fleet of workers A and B talk to different servers. Something must carry the message between them. A is connected to server 1 and B to server 2. Each server knows only its own sockets. Server 1 gets the message. It looks in its own memory and cannot find B. It saves the message and publishes it to the room's channel. Server 2 is subscribed to that room because B is in it. It receives the message and writes it to B's socket. A subscriber may reconnect to a different server. A shared event source or appropriate routing ensures that the new server can recover the right stream rather than relying only on a vanished process's memory. 00:03:56 Choose a ticker's loss policy Prices only flow one way, so server-sent events are enough. The page opens /events and shows $42.50. The price changes 200 times a second. Nobody can read that, so the server sends the latest one 4 times a second. The browser reconnects by itself. For a price only the latest value matters, so there is nothing to replay. For a display of the latest price, coalescing intermediate updates may be acceptable. For an auditable trade history, silently discarding older events would violate the product's purpose. Pick a transport after defining direction, delay and recovery. A connection that remains open still needs identity, backpressure and a way to recover messages missed while it was down.