3

I've just started to learn about channels and asgi in django .... and in few tutorials that i've seen they do this to configure the asgi apllication

asgi.py

import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mywebsite.settings')

application = ProtocolTypeRouter({
    'http':get_asgi_application(),
})

settings.py

INSTALLED_APPS = [
    'channels',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'chat'
]
ASGI_APPLICATION = 'mywebsite.asgi.application'

to check when i run my server it was supposed to be running on asgi/channel server like this

Starting ASGI/Channels version development server at http://127.0.0.1:8000/

but mine is still running on the default one

Starting development server at http://127.0.0.1:8000/

when i use daphne and put inside installed apps instead of channels

    'daphne',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'chat'
]

it works fine

Starting ASGI/Daphne version 4.0.0 development server at http://127.0.0.1:8000/

can someone tell me what is going on here?and how exactly django works with asgi?

py300
  • 51
  • 1

1 Answers1

0

The official documentation recommends adding daphne to the top of the installed apps list to ensure that it takes priority over other installed apps and is used as the server for handling Channels-based requests.

pip install -U channels["daphne"]

Daphne is the ASGI server that Django Channels uses to accept WebSocket connections. Thus, if you want to use Django Channels, you must add daphne to your installed apps. On the other hand, Channels are not required in the installed apps because they are not an application itself but a framework for creating applications. Therefore, you must use other applications that are built on the Channels framework to make use of it.

MeetGor
  • 36
  • 5