Send message using Django Channels from outside Consumer class

Firstly you need your consumer instance to subscribe to a group. from asgiref.sync import async_to_sync class GameConsumer(WebsocketConsumer): def connect(self): self.accept() self.render() async_to_sync(self.add_group)(‘render_updates_group’) controller.startTurn() … Then if you are outside of your consumer you will need to send a message to that group so that all the consumers that have registered onto the group get the … Read more

How do you authenticate a websocket with token authentication on django channels?

For Django-Channels 2 you can write custom authentication middleware https://gist.github.com/rluts/22e05ed8f53f97bdd02eafdf38f3d60a token_auth.py: from channels.auth import AuthMiddlewareStack from rest_framework.authtoken.models import Token from django.contrib.auth.models import AnonymousUser class TokenAuthMiddleware: “”” Token authorization middleware for Django Channels 2 “”” def __init__(self, inner): self.inner = inner def __call__(self, scope): headers = dict(scope[‘headers’]) if b’authorization’ in headers: try: token_name, token_key = headers[b’authorization’].decode().split() … Read more