'Error in Django channels: ValueError: No application configured for scope type 'websocket'
I am going to develop chat application using Django Channelsby following tutorials: https://channels.readthedocs.io/en/stable/tutorial/part_2.html
My Project level routing.py
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import chat.routing
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
App level routing.py file from django.urls import re_path from . import consumers
websocket_urlpatterns = [
re_path(r'^ws/chat/(?P<room_name>[^/]+)/$', consumers.ChatConsumer)
]
asgi.py file
import os
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
# Just HTTP for now. (We can add other protocols later.)
})
Solution 1:[1]
I believe that you switched the contents of project level routing.py with asgi.py file. I recreated the working version of the project according to the documentation you mentioned. Then I switched the contents of routing.py (project level) and asgi.py, and I got the same error as you on an attempt to connect to a WebSocket:
Django version 4.0.2, using settings 'django_channels_example.settings'
Starting ASGI/Channels version 3.0.4 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
HTTP GET / 200 [0.01, 127.0.0.1:53043]
WebSocket HANDSHAKING /ws/chat/lobby/ [127.0.0.1:53045]
Exception inside application: No application configured for scope type 'websocket'
Traceback (most recent call last):
The command I used in browser's JavaScript console to connect to the websocket:
webSocket = new WebSocket('ws://localhost:8000/ws/chat/lobby/');
You were also missing .as_asgi() in the app level routing.py file (i.e., chat/routing.py):
re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi())
See: https://channels.readthedocs.io/en/stable/tutorial/part_2.html#write-your-first-consumer
However, I doubt that this is what caused an error. The error you got is raised, e.g., when the 'websocket' key is missing in the routing settings (see this answer), so be careful about that too.
Also make sure that your you have this in your settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channels',
'chat'
]
...
# Where `django_channels_example` is the name of your application
WSGI_APPLICATION = 'django_channels_example.wsgi.application'
ASGI_APPLICATION = 'django_channels_example.asgi.application'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mikolaj Buchwald |
