0

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.)
})

1 Answer 1

0

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'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.