Posted under » Django on 14 July 2026
Django comes with WSGI which supports only HTTP but modern apps uses Websockets like Jupyter Notebook. To use Websockets, WSGI is not enough, you need ASGI. Websockets can handle persistent and multiple connections unlike HTTP. Because of this it is susceptible to DDOS attacks.
Django includes getting-started documentation for the following ASGI servers: Daphne, Hypercorn and Uvicorn. In this post, we will use Uvicorn although the Django Channels tutorial is using Daphne. I follow the documentation and here is the summary as the tutorial is quite long.
This is what we need while in venv.
$ pip install channels $ pip install types-channels # optional $ pip install uvicorn[standard] $ pip install websockets # optional
For some strange reason, Django v.6.0 is installed. You can do this with version 5.2+
Package Version ----------------- --------------- asgiref 3.11.1 channels 4.3.2 click 8.4.2 Django 6.0.7 django-stubs 6.0.6 django-stubs-ext 6.0.6 h11 0.16.0 pip 24.0 sqlparse 0.5.5 types-channels 4.3.0.20260518 types-PyYAML 6.0.12.20260518 typing_extensions 4.16.0 uvicorn 0.51.0 websockets 16.1
Create the chat app. We need to tell our project that the chat app is installed. Edit the settings.py file and add 'chat' to the INSTALLED_APPS setting. and others
INSTALLED_APPS = [
'channels',
'chat',
'django.contrib.admin',
...
ASGI_APPLICATION = 'd52.asgi.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer',
},
}
Create the view function for the room view.
from django.shortcuts import render
def index(request):
return render(request, "chat/index.html")
Create the template
<!-- chat/templates/chat/index.html -->
<body>
What name would you like to be?<br>
<input id="name-input" type="text" size="100"><br>
<input id="name-submit" type="button" value="Enter">
... etc.
Change the asgi.py
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'd52.settings')
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import chat.routing
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
Continue to part 2.