5 min readPublished Jul 21, 2021

Django Channels

Martin Fietkiewicz
Written by:
Martin Fietkiewicz

Django Channels are task queues that let the Django framework handle asynchronous work. Django (not Django Channels) is a server-web framework written in Python for creating database-driven websites. It revolves around the traditional request/response concept. Django framework is built on a Web Server Gateway Interface (WSGI), an interface for Python applications to handle synchronous requests. However, this server doesn't support asynchronous applications. Therefore, to work with asynchronous applications, there's a need for a server that uses Asynchronous Server Gateway Interface (ASGI) protocol that can handle WebSocket requests. This is where Django Channels (an official Django Project) fits into the picture. Channels builds upon Django's traditional ASGI support since v3.0 and provides a complete ASGI implementation for Django v2.2.

Channels incorporate a new layer and some concepts into the Django framework to augment its built-in capabilities beyond HTTP. With Channels, you can use the Django framework to handle WebSocket requests, background tasks, long-poll HTTP, and other non-request-response protocol in a way similar to traditional HTTP views. Django Channels provide developers with the choice to handle other connections in either a synchronous or asynchronous way.

Copy link to clipboardHow did Django Channels come about?

Django Channels was created by Andrew Godwin and announced as an official Django project in 2016. It was developed with the goal to extend the built-in capabilities of the Django framework to handle more than just simple HTTP requests. Channels project was initially planned to be merged into Django's core repositories for the 1.10 release, but it didn't succeed. It was then decided that the best move would be to keep Channels separate from Django's core repositories and packages but bring it under the umbrella of the Django organization on GitHub.

Even though Channels have their own release schedule and keep their backwards compatibility policies, they remain under the central Django security policy and oversight. Nowadays, Channels is available as a stand-alone app that works with the latest supported versions of Django. Channels' latest release allows integration with Django's ASGI support while continuing to work with WebSockets and other protocols that require long-running connections.

Today, Django Channels is used by Notejoy, GRVTY, deepomatic, Webscope, Inforum, and Tech Stack @ TIK, among others.

Copy link to clipboardHow does Django Channels work?

To better understand how Django Channels works, you have to be familiar with the following important terms:

  • Channel: an ordered queue (first-in-first-out) with message expiry and guaranteed at-most-once delivery to only one listener at a time.

  • Producers: the components that create and send messages to the WebSocket server. Producers communicate asynchronously with consumers by creating and pushing messages via a channel layer.

  • Consumers: the functions that receive a message (WebSocket request) from a particular producer for processing, which may return a response or not. 

  • Channel layer: the Channels' transport mechanism that passes messages from one producer to consumers. Channels uses Redis as the preferred production-quality channel layer.

  • Web Workers: the interface that processes the consumers.

  • Router: component of the channel layer that dispatches the incoming messages to the proper consumer that then processes the message.

Django Channels Framework

Source: Finally, Real-Time Django Is Here: Get Started with Django Channels

A producer pushes messages to the channel for one of the consumers listening on that channel to receive. As soon as the messages arrive from the producer, they pass through an interface server (a server that understands the ASGI protocol) and then get redirected to an ASGI router (the channel layer) that checks if it's HTTP messages or WebSocket messages. The channel layer then either sends the messages to its respective view (if HTTP) or to its consumers (if WebSocket), in a similar way to a view. When the WebSocket requests arrive, the consumer receives them and then replies with a flag accept to invoke the browser to allow the connection.  

Copy link to clipboardTechnical Overview of Django Channels

Django Channels offer six different packages that fall under the Django umbrella. These include:

  • Channels – the Django integration layer

  • Daphne – the Django Channels' HTTP, HTTP/2, and WebSocket termination server

  • Asgiref – the ASGI base libraries

  • channels_redis – the Redis channel layer backend (optional)

  • asgi_rabbitmq – the RabbitMQ channel backend for ASGI

  • asgi_ipc – the POSIX IPC channel backend for Channels 1 only (it doesn't work on Channels 2)

Channels make Django runs in three separate layers:

  • Interface servers (including a WSGI adapter and a separate WebSocket server)

  • The channel backend (a combination of pluggable Python code and a datastore used for transporting messages)

  • The workers (the layer that listens to the channels and run code for consumers)

Django Channels provides an interface server in Django that understands the ASGI protocol to give developers a choice to write their code either in synchronous or asynchronous style.

Channels provides integrations that support Django's auth system and session system among others to enable you to extend your HTTP-only project to other protocols, including WebSockets, MQTT, chatbots, amateur radio, and more. It also provides channel layers to let you communicate between processes and separate your project into different processes.

Django Channels provides Django developers with an abstraction layer that supports the implementation of WebSockets, HTTP/2, background tasks, and others. It also adds some other useful features like groups, routing, and data binding that help simplify complex tasks using Django.

Django Channels offers two basic types of channels (the core system): the routing channel (consisting of websocket.connect, websocket.receive, and websocket.disconnect), and the reply channel. The routing channel helps dispatching work to the proper consumers, while the reply channel is used to send back responses or replies.

Copy link to clipboardDjango Channels Pros:

  • Python-based open-source framework

  • Supports synchronous and asynchronous processes

  • Handle two types of requests: HTTP requests and WebSocket requests

  • Supports non-request-response protocols

  • Augments Django framework built-in functionalities

Copy link to clipboardDjango Channels Cons:

Copy link to clipboardRead more…

Join the Ably newsletter today

1000s of industry pioneers trust Ably for monthly insights on the realtime data economy.
Enter your email