O'Reilly logo

Lightweight Django by Mark Lavin, Julia Elman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Real-Time Django

There has been a recent trend toward building real-time web applications or integrating real-time components into existing applications. However, Django is primarily optimized for short-lived HTTP request/response cycles, and most Python web servers are not made for handling a large number of concurrent and long-lived connections required to support real-time applications. The rise of Node.js and the inclusion of asyncio in the Python standard library have many developers using cooperative multitasking and event loops for a solution to the high-concurrency problem. However, this solution requires the entire stack to be written in this cooperative fashion to ensure that nothing blocks the loop. There is no easy way to make this drop into an existing Django application. Beyond inefficient long polling, there is no obvious solution to add real-time features to a Django application.

In this chapter, we’ll explore how to integrate real-time features into the same task-board application backed by the Django-based REST API. The real-time features will be handled by a new server, written using Tornado, that uses asynchronous I/O to handle a large number of concurrent connections. We’ll learn how to effectively and securely allow Django to push updates to the client. First we’ll examine the set of new web APIs defined in HTML5 to better understand this approach.

HTML5 Real-Time APIs

We will incorporate real-time updates to our application to let users see tasks moving ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required