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

Question:

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:UsersADMINAppDataLocalProgramsPythonPython310libsite-packageschannelsstaticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "C:UsersADMINAppDataLocalProgramsPythonPython310libsite-packageschannelsrouting.py", line 71, in __call__
    return await application(scope, receive, send)
  File "C:UsersADMINAppDataLocalProgramsPythonPython310libsite-packageschannelsrouting.py", line 150, in __call__
    return await application(
  File "C:UsersADMINAppDataLocalProgramsPythonPython310libsite-packagesasgirefcompatibility.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
Asked By: Jaswanth Ch

||

Answers:

change :

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

to :

ws_patterns = [
    path('ws/test/',TestConsumer.as_asgi())
]
Answered By: monim