Long Polling with Tornado

Another advantage of Tornado’s asynchronous architecture is the ease with which it handles HTTP long polling. This is a way of handling real-time updates, which can be used for effects as simple as a notification badge and as complex as multi-user chat rooms.

Developing web applications that offer real-time updates is a constant challenge for web programmers. Updating a user’s status, sending new message notifications, or indicating any other global activity all require a method for the server to send messages to the browser after the initial document has finished loading. One early approach was for the browser to poll the server for new updates at a regular interval. This technique poses obvious challenges: the polling frequency must be fast enough that notifications are up-to-date, but not too frequent that the HTTP requests pose serious scaling challenges when hundreds or thousands of clients continually open new connections. Frequent polling presents a “death by a thousand cuts” strain on a web server.

So-called “server push” technology allows web applications to distribute updates in real time while maintaining reasonable resource usage and ensuring predictable scaling. For a server push technology to be practical, it must play nicely with existing browsers. The most popular technique is to emulate a server pushing updates by letting the browser initiate the connection. These sorts of HTTP connections are called long polling, or Comet requests.

Long polling ...

Get Introduction to Tornado now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.