Skip to content Skip to sidebar Skip to footer

Django Channels Group Send Only Sends The Message To Last Channel

I'm working on django channels-3.0.3, the group_send only sends my message to the last connected channel to the connected users times. settings ... INSTALLED_APPS = [ 'django.c

Solution 1:

Try this:

  1. Import get_asgi_application from django.core.asgi.
  2. allow Django's ASGI application to handle traditional http requests by adding http as key & get_asgi_application() as the value of key in the dictionary inside ProtocolTypeRouter.
  3. use/call the as_asgi() classmethod while routing the ChatConsumer consumer.

routing.py

from django.conf.urls import url
from django.core.asgi import get_asgi_application #Change No.1

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator

from chat.consumers import ChatConsumer

application = ProtocolTypeRouter({
    "http": get_asgi_application(), #Change No.2
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter([
                url(r"^messages/(?P<username>[\w.@+-]+)/$", ChatConsumer.as_asgi()), #Change No.3
           ])
        )
    )
})

More Information on channels.readthedocs.io.


Post a Comment for "Django Channels Group Send Only Sends The Message To Last Channel"