question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AuthMiddlewareStack does not work with AsyncHttpConsumers in Channels 3

See original GitHub issue

Hello,

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:closed
  • Created 3 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
carltongibsoncommented, Nov 4, 2020

@gghildyal Super. Good to hear! Thanks for the confirmation.

3.0.1 will be as soon as I can get the moment.

2reactions
KStenKcommented, Oct 31, 2020

I am able to access self.scope["user"] with the following routing:

application = ProtocolTypeRouter(
    {
        "websocket": AuthMiddlewareStack(
            URLRouter(
                [
                    re_path(r"^mywebsocketurl/$", MyConsumer.as_asgi()),
                ]
            )
        ),
        "http": get_asgi_application(),
    }
)

Python 3.9.0

Dependencies: Django==3.1.2 channels==3.0.0 channels-redis==3.2.0 aioredis==1.3.1 hiredis==1.1.0

Not installed: django-redis==4.12.1 redis==3.5.3

Edited: works with Python 3.8.6 too.

Hei @ipaleka,

I am using AsyncHttpConsumer consumer, which I understand has to be under “http” key in the ProtocolTypeRouter.

Tried to put my LogsSSEConsumer under the “websocket” key, but then that route is not found.

Read more comments on GitHub >

github_iconTop Results From Across the Web

channels 3.0.0: Only last connected socket receives ... - GitHub
I have tested with Django 3.1 and Django 3.1.2 and Channels3. ... AuthMiddlewareStack does not work with AsyncHttpConsumers in Channels 3 # ...
Read more >
Channels Documentation
Channels is a project that takes Django and extends its abilities beyond HTTP - to handle WebSockets, chat protocols,.
Read more >
Django Channels send group message from Celery task ...
In the end I couldn't solve the problem and choose an alternative solution using a Channels AsyncHttpConsumer to send the group message.
Read more >
django-channels - Bountysource
I am migrating an app to Django 3.1.5 and channels 3.0.3 and I am having problems with websockets getting disconnected around 2 seconds...
Read more >
subject:"Re\: Django channels" - The Mail Archive
I can confirm chat example in channel working fine for me. ... But it's not sable and throwing an exception after 2-3 hours....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found