Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Django channel:
Channels is a project that takes Django and extends its abilities beyond HTTP – to handle WebSockets, chat protocols, IoT protocols, and more.
Pre-requesting :
- Python and Pip
- Django
- Basic Knowledge of creating Django projects.
Create a Chat App:
In Terminal, create a new Django project named chat app
Navigate to the chat app directory and create an app:
Add chat app in installed apps in setting.py
Create templates/chat/index.html
file to run chat UI.
chat/
__init__.py
templates/
chat/
index.html
views.py
Copy-paste below code in index.html
file.
Explanation:
This HTML code is to create a form with one input field to name the room and submit button. Once the submit button is pressed, the new room is created and redirected to that page.
Sample Picture of the HTLM code
Create the view function for the room view. Put the following code in chat/views.py
:
Create urls.py
file in chat folder and include the following code:
This creates a route “/chat” which render the index.html page.
Include chat/urls.py in chatapp/urls.py.
Intergrate Channel in chatapp:
Install channel:
Once that’s done, you should add channels to your INSTALLED_APPS setting:
Then, adjust your project’s asgi.py
file, e.g. myproject/asgi.py
, to wrap the Django ASGI application:
And finally, set your ASGI_APPLICATION setting to point to that routing object as your root application:
Run the server and check ASGI/channel started the server
Create a room.html file in the templates/chat directory and include the following code.
Device 1 ( from Chrome )
Device 2 ( from Safari )
Create the view function for the room view in chat/views.py:
Create the route for the room view in chat/urls.py:
Write your first consumer
When Django accepts an HTTP request, it consults the root URLconf to lookup a view function and then calls the view function to handle the request. Similarly, when Channels accepts a WebSocket connection, it consults the root routing configuration to lookup a consumer and then calls various functions on the consumer to handle events from the connection.
We will write a basic consumer that accepts WebSocket connections on the path /ws/chat/ROOM_NAME/ that takes any message it receives on the WebSocket and echos it back to the same WebSocket.
Create a new file chat/consumers.py
and include the following code:
We need to create a routing configuration for the chat app that has a route to the consumer. Create a new file chat/routing.py and include code.
We call the as_asgi() the class method in order to get an ASGI application that will instantiate an instance of our consumer for each user connection. This is similar to Django’s as_view(), which plays the same role for per-request Django view instances.
(Note we use re_path() due to limitations in URLRouter.)
The next step is to point the root routing configuration at the chat.routing module. In mysite/asgi.py
, import AuthMiddlewareStack, URLRouter, and chat.routing; and insert a ‘websocket
’ key in the ProtocolTypeRouter list in the following format:
Migrate and run the server.
Enable a channel layer
A channel layer is a kind of communication system. It allows multiple consumer instances to talk with each other, and with other parts of Django.
A channel layer provides the following abstractions:
- A channel is a mailbox where messages can be sent to. Each channel has a name. Anyone who has the name of a channel can send a message to the channel.
- A group is a group of related channels. A group has a name. Anyone who has the name of a group can add/remove a channel to the group by name and send a message to all channels in the group. It is not possible to enumerate what channels are in a particular group.
Install channel redis python package
In settings.py
add channel layer
Now that we have a channel layer, let’s use it in ChatConsumer. Put the following code in chat/consumers.py
, replacing the old code:
Start Redis server :
Start Django server and test it.
Make our chat app asynchronous consumers.py
Send data from view.py
add the following code in view.py
add a function in consumer to send data
Summary:
In this use case, we made a very basic chat app using the Django channel. Using this the pub/sub can be created by the same method.
Hi, this is a comment.
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
Commenter avatars come from Gravatar.