In Channels version 3.0.0 python raise exception when I try connect by websocket to my Django Application . Console log from runserver:
November 01, 2020 - 13:18:04
Django version 3.1.2, using settings 'Menu.settings'
Starting ASGI/Channels version 3.0.0 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
WebSocket HANDSHAKING /ws/dist [127.0.0.1:56024]
Exception inside application: object.__init__() takes exactly one argument (the instance to initialize)
Traceback (most recent call last):
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
return await application(scope, receive, send)
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/channels/sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/channels/sessions.py", line 172, in __call__
return await self.inner(self.scope, receive, self.send)
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/channels/auth.py", line 181, in __call__
return await super().__call__(scope, receive, send)
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/channels/middleware.py", line 26, in __call__
return await self.inner(scope, receive, send)
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/channels/routing.py", line 150, in __call__
return await application(
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/asgiref/compatibility.py", line 33, in new_application
instance = application(scope)
File "/home/nakmak98/source/Menu/pycharm_venv/lib/python3.8/site-packages/channels/generic/websocket.py", line 23, in __init__
super().__init__(*args, **kwargs)
index.js
var ws = new WebSocket( 'ws://' + window.location.host + '/ws/dist')
routing.py
from django.urls import path
from . import consumers
websocket_urlpatterns = [
path('ws/dist', consumers.MealPointConsumer),
]
When I run my code on previous version of Channels, it works great.
Django version 3.1.2, using settings 'Menu.settings'
Starting ASGI/Channels version 2.4.0 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
HTTP GET /dist/ 200 [0.14, 127.0.0.1:55192]
...
WebSocket HANDSHAKING /ws/dist [127.0.0.1:55196]
WebSocket CONNECT /ws/dist [127.0.0.1:55196]
My OS: Linux Mint 19.3
pip freeze output:
aioredis==1.3.1
asgiref==3.2.10
async-timeout==3.0.1
attrs==20.2.0
autobahn==20.7.1
Automat==20.2.0
cffi==1.14.3
channels==3.0.0
channels-redis==3.2.0
constantly==15.1.0
cryptography==3.2.1
daphne==3.0.0
Django==3.1.2
hiredis==1.1.0
hyperlink==20.0.1
idna==2.10
incremental==17.5.0
msgpack==1.0.0
Pillow==8.0.1
pkg-resources==0.0.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
PyHamcrest==2.0.2
pyOpenSSL==19.1.0
pytz==2020.1
service-identity==18.1.0
six==1.15.0
sqlparse==0.4.1
Twisted==20.3.0
txaio==20.4.1
zope.interface==5.1.2
v3 is a big update to ASGI v3, which changes the signuature of consumers.
path('ws/dist', consumers.MealPointConsumer),
Here you need to add the new as_asgi() call when routing your consumer.
See the 3.0 release notes for more details and guidance on updating:
https://channels.readthedocs.io/en/stable/releases/3.0.0.html#update-to-asgi-3
You should also probably review the docs, and update to routing.py to asgi.py as you go, to fall in-line with Django's conventions.
I'll leave this open for the now in case others bump into similar issues.
That release notes link again: https://channels.readthedocs.io/en/stable/releases/3.0.0.html#update-to-asgi-3
Perhaps it could be worthwhile to pin this issue for a while?
i'm a little confused by the incopatiblities....
I have the same error that was mentioned above.
I am using generic consumer and the routings and have actually adapted all the changes from the release notes. Except the block to "channels.middleware.BaseMiddleware". Can this be the reason. What is meant here?
@carltongibson it took forever to find your comment, but adding as_asgi() saved me a lot of time. Thanks!
Most helpful comment
v3 is a big update to ASGI v3, which changes the signuature of consumers.
Here you need to add the new
as_asgi()call when routing your consumer.See the 3.0 release notes for more details and guidance on updating:
https://channels.readthedocs.io/en/stable/releases/3.0.0.html#update-to-asgi-3
You should also probably review the docs, and update to
routing.pytoasgi.pyas you go, to fall in-line with Django's conventions.I'll leave this open for the now in case others bump into similar issues.
That release notes link again: https://channels.readthedocs.io/en/stable/releases/3.0.0.html#update-to-asgi-3