AuthMiddlewareStack does not work with AsyncHttpConsumers in Channels 3
See original GitHub issueHello,
I upgrade Django channels to ver. 3.0.0 and discovered that few features stopped working for me. This is happening in the manage.py runserver command. Haven’t tried in staging/production yet.
In Django channels ver. 2.4.0, I had the routing.py
configuration file set up like this:
from django.urls import re_path
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.http import AsgiHandler
from apps.logs.ws import consumers as logs_consumers
application = ProtocolTypeRouter({
"http": AuthMiddlewareStack(
URLRouter([
re_path(r"^admin/logs/live/$", logs_consumers.LogsSSEConsumer),
re_path(r"", AsgiHandler)
])
)
})
The LogsSSEConsumer
consumer class is inheriting from AsyncHttpConsumer.
Everything was working on ver. 2.4 and I could access to self.scope[“user”] in the consumers handle method.
Now upgrading to channels ver. 3 I updated asgi.py
file like this:
import os
from django.urls import re_path
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from apps.logs.ws import consumers as logs_consumers
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = ProtocolTypeRouter({
"http": AuthMiddlewareStack(
URLRouter([
re_path(r"^admin/logs/live/$", logs_consumers.LogsSSEConsumer.as_asgi()),
re_path(r"", get_asgi_application()),
])
),
})
Everything is broken now, a lot of 500 errors showing up in the browser and nothing is working. In the terminal, there are no errors.
If I remove AuthMiddlewareStack wrapper, then everything starts working, except LogsSSEConsumer
consumer, because i cannot access self.scope[“user”] anymore.
If I remove self.scope[“user”] lines from the consumers handler method, then everything is working again.
I don’t know, am I setting up those routes wrongly or how can I set up now AsyncHttpConsumer consumers so I can get access to self.scope[“user”].
Same question is, how can i setup AllowedHostsOriginValidator wrapper with AsyncHttpConsumer consumers?
OS: Docker - python:3.8.6-slim Dependencies: Django==3.1.2 channels==3.0.0 channels_redis==3.1.0 django-redis==4.12.1 redis==3.5.3 …
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (10 by maintainers)
@gghildyal Super. Good to hear! Thanks for the confirmation.
3.0.1 will be as soon as I can get the moment.
Hei @ipaleka,
I am using
AsyncHttpConsumer
consumer, which I understand has to be under “http” key in theProtocolTypeRouter
.Tried to put my
LogsSSEConsumer
under the “websocket” key, but then that route is not found.