Home > Net >  TypeError: AsyncConsumer.__call__() missing 1 required positional argument: 'send
TypeError: AsyncConsumer.__call__() missing 1 required positional argument: 'send

Time:09-18

Well I am using channels as WebSocket and redis as storage to build chat application in Django.so to accomplish connection between the websocketconsumer with asgi file. I am trying to run the django server on windows but I am getting the following error, help me.

Error

Exception inside application: AsyncConsumer.__call__() missing 1 required positional argument: 'send'
Traceback (most recent call last):
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python310\lib\site-packages\channels\staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python310\lib\site-packages\channels\routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python310\lib\site-packages\channels\routing.py", line 150, in __call__
    return await application(
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python310\lib\site-packages\asgiref\compatibility.py", line 34, in new_application
    return await instance(receive, send)
TypeError: AsyncConsumer.__call__() missing 1 required positional argument: 'send'
WebSocket DISCONNECT /ws/test/ [127.0.0.1:59461]

asgi.py

"""
ASGI config for chatapp project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter,URLRouter
from django.urls import path
from home.consumers import *

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chatapp.settings')

application = get_asgi_application()
ws_patterns = [
    path('ws/test/',TestConsumer)
]

application = ProtocolTypeRouter({
    'websocket' : URLRouter(ws_patterns)
})

consumers.py

from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
import json
class TestConsumer ( WebsocketConsumer ) :
    def connect(self):
        self.room_name = "test_consumer" 
        self.room_group_name="test_consumer_group"
        async_to_sync(self.channel_layer.group_add)(
            self.room_name,self.room_group_name
        )
        self.accept()
        self.send(text_data=json.dumps({'status':'connected'}))
    def receive(self):
        pass

    def disconnect(self):
        pass

CodePudding user response:

change :

ws_patterns = [
    path('ws/test/',TestConsumer)
]

to :

ws_patterns = [
    path('ws/test/',TestConsumer.as_asgi())
]
  • Related