Django Channels

Channels is a project that uses Django and adds some functionalities beyond the traditional HTTP by using WSGI and ASGI.  We can use both synchronous and asynchronous requests with ASGI but only synchronous with WSGI. Django can handle the only HTTP, while with Channels, we can handle Web Sockets, IoT protocols, Chat protocols, and many more. While using Channels, we have a choice to handle connections in either synchronous or asynchronous ways. Channels take the core of Django and layer a fully asynchronous layer underneath while running Django synchronously but handling connections and socket asynchronously.

The advantage of using WebSockets over HTTP is the WebSockets and we can manage the communication with the client and server. It allows bi-directional communications meaning that the server can send data to the client without needing the user's permission. HTTP can only make the server send data when a user requests it. They establish a long head Transmission Control Protocol socket connections between server and user. This results in minimum latency.

There are many real-world applications that use web sockets like multiplayer games, chat apps, the internet of things, and many more.

Installation

We can install Channels by PyPI- run this command to install:

python –m pip install –U channels

After it gets installed, add channels to the installed apps:

INSTALLED_APPS = (
‘django.contrib.auth’,
…….
'channels',
)

Now, after adding channels to the installed apps, we have to change asgi.py to use Django ASGI:

import os


CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [CHANNEL_REDIS_HOST],
"symmetric_encryption_keys": [SECRET_KEY],
},
},
}

Finally, set the ASGI_APPLICATION parameter to that routing object as the root application:

ASGI_APPLICATION: “my_project.asgi.application"

Now installation is complete, and channels are integrated into the Django.

Principle

The principle of Django, which it works on, is called "turtles all the way down" – in which each consumer is considered to be an application.

There are tools in channels by which we can write these basic consumers – individual components for the handling of chat messages and notifications and later tie them up by routing the URL, protocol detection, and other handy things.

HTTP and Django applications are a part of the bigger picture. We can still use them with channels with Django's native ASGI approach, but we can also write WebSocket receivers or custom HTTP long polling and have that code with the existing one.

Channels view is to give its users the ability to use synchronous processing for Django views, but also have the option of asynchronous interface for complex tasks.

Scopes and Events

One of the operational functions of channels is that it split up the connections requests into two parts which are server and a series of events.

The scope is nothing but the information and details about the incoming connection request like the path from which the web request was made form or the IP address of the WebSocket- it usually persists throughout the connection.

For the HTTP, the lifetime of scope is just a single request, while for WebSockets, it can persist for the lifetime of the sockets. In the case of the other protocols, it depends on the ASGI configuration of the protocol.

In a scope, many events occur, which includes the interaction of the user with the server-side application and making some HTTP requests. In a channel, it will restart once per scope. After that, it captures a series of events which, therefore, decides the functionality after it.

Example of scope with a chatbot:

•          First step is the user sending a message.

•          It will create a scope with the user's username, nickname, and user ID.

•          The action of the user will create a chat.recieved_message event, although it not compulsory to respond, it can do it by sending single or multiple chat messages back as chat.send_messages events.

•          This cycle will continue as the user will send more messages.

•          Scope will be restarted once an application instance is closed.

Consumer

A consumer is the basic unit of channels. What a consumer does is consumes the events coming from the client side. Once an event is received, it will find the right consumer for that request.

Consumers live for the duration of scope, so they are long-running, unlike Django views. Although, they can be short running as well when HTTP requests are served by consumers.

The code for basic Consumer looks like:

class ChatConsumer(WebsocketConsumer):


def connect(self):
self.username = "S1mple"
self.accept()
self.send(text_data="[ Welcome %s!]" % self.username)


def receive(self, *, text_data):
if text_data.startswith("/name"):
self.username = text_data[5:].strip()
self.send(text_data="[choose username %s]" % self.username)
else:
self.send(text_data=self.username + ": " + text_data)


def disconnect(self, message):
pass

Every scope has different events related to it.

As a fully  asynchronous loop event loop is running underneath the channels, we can write code as synchronous like above and do an operation like connecting to the Django ORM safely, or if we want full control, we can run it asynchronously as well like below:

class ChatConsumer(AsyncConsumer):
    async def websocket_connect(self, message):
        await self.send({
            "type": "websocket.accept",
        })


    async def websocket_receive(self, message):
        await asyncio.sleep(2)
        await self.send({
            "type": "websocket.send",
            "text": "pong",
        })

Cross process communications

Each socket inside the application is run by an application instance inside servers like WSGI or WebSockets. Once they get called, they can send data back to the client directly without him prompting for it.

In Channels, we do this by channel layer, which allows the server to send the information between different processes. Every application has a unique channel name and allows point–to–point or broadcasting messages.

Django Integration

Channels can be easily integrated with Django to support common features like sessions and authentications. We can also combine sessions with WebSockets by using middlewares.