Home > OS >  Django Channels is not taking over deployment server
Django Channels is not taking over deployment server

Time:01-10

I am attempting Django Channels for the first time. I am following this tutorial - https://www.youtube.com/watch?v=cw8-KFVXpTE&t=21s - which basically explains Channels basics. I installed Channels in my virual environment using pip install channels and it installed the latest version, which is 4.0.0. I have laid out the basic setup. But when I run python manage.py runserver, I get -

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

when I shoul be getting this -

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting ASGI/Channels version 4.0.0 development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

I cannot figure out what I might be doing wrong here and I cannot find any relevant solution anywhere. I would really appreciate any suggestions anyone might have.

settings.py

INSTALLED_APPS = [
    'channels',
    
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',

    'Orders',
]

ASGI_APPLICATION = "BrokerBackEnd.asgi.application"

asgi.py

import os

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
import Orders.routing as route

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

application = ProtocolTypeRouter({
    'http': get_asgi_application(),
    
    'websocket': URLRouter(
            route.websocket_urlpatterns
        )
})

consumer.py

class OrderConsumer(WebsocketConsumer):
    def connect(self):
        self.accept()
    
    def receive(self, text_data=None, bytes_data=None):
        self.send(text_data="Message = "   text_data)
    
    def disconnect(self, code):
        return super().disconnect(code)

routing.py

websocket_urlpatterns = [
    re_path(r"socket/order/", OrderConsumer.as_asgi())
]

CodePudding user response:

As of django-channels 4.0.0 the daphne server was decoupled from the rest of channels. Now, you need to include daphne in your settings.py INSTALLED_APPS if you wish to use it. Include this at the top of your installed apps. The tutorial you are looking at uses an older version of channels.

INSTALLED_APPS = [
    'daphne',
    'channels',
    
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',

    'Orders',
]

ASGI_APPLICATION = "BrokerBackEnd.asgi.application"

Also, make sure daphne is actually installed:

pip install -U channels["daphne"]
  • Related