Tag: ajax

  • How the live comment stream works

    Open any film in two browsers and type a comment in one. It appears in the other within about three seconds, and the viewer count updates at the same time. Here is what is actually happening.

    Each open page asks the server a question every three seconds: is there anything newer than comment number N, and who else is here? The browser sends the highest comment id it already holds, so the server replies with only what is new — usually nothing at all, which costs a few hundred bytes.

    Comments and the viewer count are served by one request, not two. They refresh on the same schedule and appear in the same panel, so splitting them would have doubled the traffic for no benefit.

    The count works because every browser generates a random token and sends it with each poll. The server records when it last heard from each token and forgets any that go quiet for thirty seconds. Closing a tab sends nothing, so silence is the only signal available.

    Polling is not the most elegant answer — WebSockets would push instead of asking. But polling works everywhere, needs no extra server process, and for a comment stream three seconds late is not late at all.