R
R
rolexi24362020-11-03 10:07:49
Django
rolexi2436, 2020-11-03 10:07:49

How to connect to a socket in Django Channels?

When connecting to a socket, it gives the following error:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 03, 2020 - 07:02:03
Django version 3.1.3, using settings 'my_chat.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/chat/ [127.0.0.1:43390]
Exception inside application: object.__init__() takes exactly one argument (the instance to initialize)
Traceback (most recent call last):
  File "/home/sus/Desktop/test/env/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/home/sus/Desktop/test/env/lib/python3.8/site-packages/channels/routing.py", line 150, in __call__
    return await application(
  File "/home/sus/Desktop/test/env/lib/python3.8/site-packages/asgiref/compatibility.py", line 33, in new_application
    instance = application(scope)
  File "/home/sus/Desktop/test/env/lib/python3.8/site-packages/channels/generic/websocket.py", line 23, in __init__
    super().__init__(*args, **kwargs)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
WebSocket DISCONNECT /ws/chat/ [127.0.0.1:43390]


At first I wrote the code according to the documentation, it did not work with this error.
I tried to run the one already prepared from here https://github.com/webmagicc/my_chat/tree/29ee154d... , the error persisted.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rolexi2436, 2020-11-04
@rolexi2436

I found out that this is due to updating channels to version 3.0.
Now in routing you need to additionally use the method as_asgi()
url(r'^ws/chat/$', ChatConsumer.as_asgi()),
Instead of
url(r'^ws/chat/$', ChatConsumer),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question