For your reference:

change from in

websocket_urlpatterns = [
    re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer),

to Consumers now have an as_asgi() class method you need to call when setting up your routing:

websocket_urlpatterns = [
    re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi()),

then if you need custom authentication

from channels.auth import AuthMiddlewareStack
from channels.db import database_sync_to_async
from django.contrib.auth import get_user_model

User = get_user_model()

def get_user(user_id):
        return User.objects.get(id=user_id)
    except User.DoesNotExist:
        return AnonymousUser()
class QueryAuthMiddleware:
    Custom middleware (insecure) that takes user IDs from the query string.
    def __init__(self, app):
        # Store the ASGI application we were passed = app
    async def __call__(self, scope, receive, send):
        # Look up user from query string (you should also do things like
        # checking if it is a valid user ID, or if scope["user"] is already
        # populated).
        scope['user'] = await get_user(int(scope["query_string"]))
        return await, receive, send)
TokenAuthMiddlewareStack = lambda inner: QueryAuthMiddleware(AuthMiddlewareStack(inner))   

use requirements.txt as following list, and also download package in this order


CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top