18

When I run the project through the gunicorn (command: gunicorn --bind :8000 myproject.asgi:application), I get this error.

Error

Traceback (most recent call last):
  File "/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 136, in handle
    self.handle_request(listener, req, client, addr)
  File "/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() missing 1 required positional argument: 'send'

asgi.py

import os
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.base')
django_asgi_app = get_asgi_application()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter

from channels.security.websocket import AllowedHostsOriginValidator
from chats import routing

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                routing.websocket_urlpatterns
            )
        )
    ),
})

chats.routing.py

from django.urls import re_path

from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/messages/(?P<username>[-:\w]+)/$', consumers.ChatConsumer.as_asgi()),
]

Sultan Syidaliev
  • 181
  • 1
  • 1
  • 3
  • Give more details: Django version, are you using some cloud service?.. Please review this: https://stackoverflow.com/help/how-to-ask – Carmoreno Jun 30 '21 at 12:55
  • 3
    I am getting same issue, did you find any solution? – washiur17 Aug 02 '21 at 17:31
  • I have filed an issue on channels official github site: https://github.com/django/channels/issues/1735 – George Y Aug 08 '21 at 09:35
  • for me, I'm working on pytest and using `WebsocketCommunicator` and I missing to add `.as_asgi()` on it, `WebsocketCommunicator(AnnouncementConsumer.as_asgi(), f"/announcement/?token={auth_token.key}")` – binpy Jun 02 '22 at 04:45

1 Answers1

19

There is an issue on github similar to your problem

Answer: Likely you missed the as_asgi() in your routing.py

George Y
  • 525
  • 3
  • 14
  • but you are using `consumers.ChatConsumer.as_asgi()`, so maybe the syntax is wrong, or you could file another issue there? – George Y Aug 09 '21 at 03:20
  • I have already solved mine by adding `.as_asgi()` after my path. Don't know what makes your problem unsolved. – George Y Dec 13 '21 at 11:49