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 ...

Get Lightweight Django 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.