A Django Channels WebSocket closes with 1006 before it ever opens
Your error
WebSocket connection to 'ws://127.0.0.1:8000/ws/chat/lobby/' failed4 more lines it can showFix it fast
Most common cause, 1 of 3runserver is still Django's WSGI server because daphne is not installed as an app
Install Channels with its Daphne extra, put "daphne" first in INSTALLED_APPS, and point ASGI_APPLICATION at the application object in your asgi.py.
python -m pip install -U 'channels[daphne]'
# settings.py
INSTALLED_APPS = [
"daphne",
"django.contrib.admin",
"django.contrib.staticfiles",
# ... your apps
]
ASGI_APPLICATION = "mysite.asgi.application"Short answer
Close code 1006 means the browser saw the connection drop with no close frame, so the server never accepted the handshake. With Django Channels 4.3 the usual reasons are runserver still being plain WSGI because daphne is missing from INSTALLED_APPS, a websocket route that does not match the path, or AllowedHostsOriginValidator rejecting the page's origin. The runserver log shows which.
Which cause do you have?
Start the development server and open the page that connects. The banner and the lines printed during the connect attempt point to one cause each: a banner without ASGI/Daphne in it is cause 1, a ValueError is cause 2, a WSREJECT line is cause 3.
python -m pip show channels daphne
python manage.py runserverThen find the line you see in this list and open its fix:
- 1runserver is still Django's WSGI server because daphne is not installed as an app
The banner reads Starting development server at ... (Starting WSGI development server on newer Django) instead of Starting ASGI/Daphne version ... development server - 2asgi.py has no websocket entry, or the route pattern never matches
The server log shows ValueError: No application configured for scope type 'websocket', or No route found for path '...' - 3AllowedHostsOriginValidator refuses the page's Origin
The log prints WSCONNECTING then WSREJECT for the path, and the front end runs on a host or port that is not in ALLOWED_HOSTS
What does the error look like?
WebSocket connection to 'ws://127.0.0.1:8000/ws/chat/lobby/' failedValueError: No route found for path 'ws/chat/lobby/'.ValueError: No application configured for scope type 'websocket'WSREJECT /ws/chat/lobby/Daphne must be listed before django.contrib.staticfiles in INSTALLED_APPS.
Cause 1 of 3
runserver is still Django's WSGI server because daphne is not installed as an app
You seeThe banner reads Starting development server at ... (Starting WSGI development server on newer Django) instead of Starting ASGI/Daphne version ... development server
Check
Django's own runserver only speaks WSGI, so the WebSocket upgrade never reaches Channels and the browser reports 1006. Since Channels 4 the ASGI runserver lives in the daphne package and is only used when daphne is in INSTALLED_APPS, ahead of django.contrib.staticfiles. A missing entry, or one placed too late, leaves the WSGI server in charge.
python manage.py checkFix
Install Channels with its Daphne extra, put "daphne" first in INSTALLED_APPS, and point ASGI_APPLICATION at the application object in your asgi.py. Any other app that also overrides runserver must sit below daphne or be removed.
python -m pip install -U 'channels[daphne]'
# settings.py
INSTALLED_APPS = [
"daphne",
"django.contrib.admin",
"django.contrib.staticfiles",
# ... your apps
]
ASGI_APPLICATION = "mysite.asgi.application"Verify
Restart the server. The banner now names ASGI/Daphne and the log prints a WSCONNECT line when the page opens the socket.
python manage.py runserverConfirmed by channels.readthedocs.io
Cause 2 of 3
asgi.py has no websocket entry, or the route pattern never matches
You seeThe server log shows ValueError: No application configured for scope type 'websocket', or No route found for path '...'
Check
ProtocolTypeRouter raises the first error when its dictionary has only an http key. URLRouter raises the second when none of its patterns match; it strips the leading slash before matching, so a pattern written as ^/ws/... can never succeed, and a path the JavaScript opens with a different prefix fails the same way.
grep -n "websocket\|URLRouter\|re_path\|path(" mysite/asgi.py chat/routing.pyFix
Add a websocket key that wraps your URL routes, and write each route without a leading slash, exactly as the browser requests it.
# mysite/asgi.py
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django_asgi_app = get_asgi_application()
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from chat.routing import websocket_urlpatterns
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
),
})
# chat/routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r"ws/chat/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()),
]Verify
Reload the page. The ValueError is gone and the socket's onopen handler runs.
python manage.py runserverConfirmed by raw.githubusercontent.com
Cause 3 of 3
AllowedHostsOriginValidator refuses the page's Origin
You seeThe log prints WSCONNECTING then WSREJECT for the path, and the front end runs on a host or port that is not in ALLOWED_HOSTS
Check
AllowedHostsOriginValidator compares the Origin header with ALLOWED_HOSTS and denies anything else; Daphne then answers the handshake with HTTP 403, and the browser reports a close with no close frame, code 1006. With DEBUG on and ALLOWED_HOSTS empty it lets only localhost, 127.0.0.1 and [::1] through, so a page served from a LAN IP or another domain is turned away.
python manage.py shell -c "from django.conf import settings; print(settings.DEBUG, settings.ALLOWED_HOSTS)"Fix
Add the host the page is served from to ALLOWED_HOSTS. When the front end lives on another origin than Django, wrap the websocket app in OriginValidator with that origin listed instead.
# settings.py
ALLOWED_HOSTS = ["127.0.0.1", "localhost", "app.example.com"]
# or, in asgi.py, for a front end on another origin
from channels.security.websocket import OriginValidator
"websocket": OriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns)),
["https://app.example.com"],
),Verify
The log now shows WSCONNECT instead of WSREJECT for the same path.
python manage.py runserverConfirmed by channels.readthedocs.io
How do I stop it coming back?
- Keep daphne as the first item in INSTALLED_APPS and let the daphne.E001 system check run in CI with python manage.py check.
- Start every WebSocket route with a shared ws/ prefix and keep the JavaScript URL in one constant built from the same prefix.
- List each front-end host in ALLOWED_HOSTS through an environment variable, so staging and production origins are added when the domain is.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.