I am trying to connect django channel 3 but it is connection rejected


import os
from .consumer import MyConsumer
from django.urls import path
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")

django_asgi_app = get_asgi_application()

application = ProtocolTypeRouter(
        "http": django_asgi_app,
        "websocket": AllowedHostsOriginValidator(


from channels.generic.websocket import WebsocketConsumer

class MyConsumer(WebsocketConsumer):
    groups = ["broadcast"]

    def connect(self):

    def receive(self, text_data=None, bytes_data=None):
        self.send(text_data="Hello world!")
        self.send(bytes_data="Hello world!")   

    def disconnect(self, close_code):

Redis also tried inmemory

    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("", 6379)],

console output

WebSocket HANDSHAKING /ws/chat/ []
WebSocket REJECT /ws/chat/ []
WebSocket DISCONNECT /ws/chat/ []

Well, there’s a lot that can be the root cause of your issue.

Since you have:

  • AllowedHostsOriginValidator activated, what are your ALLOWED_HOSTS setting?
  • AuthMiddlewareStack activated, are you logged in?

This appears to be a duplicate of I am trying to connect django channel but it is rejected my request and a comment you made in a separate thread.

Please do not spam this forum with repeated requests for assistance on the same topic. If you have additional information to provide, post that information as comments on the original thread.

See FAQ - Django Forum