django / daphne

Django Channels HTTP/WebSocket server
BSD 3-Clause "New" or "Revised" License
2.32k stars 256 forks source link

Exception inside application: 'module' object is not callable #404

Closed sohambhardwaj closed 2 years ago

sohambhardwaj commented 2 years ago

My django-channels app works fine using manage.py runserver but when I am trying to run through daphne it is showing me the following error on running daphne -b 0.0.0.0 -p 8000 djangoProject.asgi:stream

2022-02-04 14:48:57,380 INFO     Starting server at tcp:port=8000:interface=0.0.0.0
2022-02-04 14:48:57,380 INFO     HTTP/2 support enabled
2022-02-04 14:48:57,380 INFO     Configuring endpoint tcp:port=8000:interface=0.0.0.0
2022-02-04 14:48:57,381 INFO     Listening on TCP address 0.0.0.0:8000
127.0.0.1:55961 - - [04/Feb/2022:14:49:03] "WSCONNECTING /ws/percentage/" - -
2022-02-04 14:49:04,388 ERROR    Exception inside application: 'module' object is not callable
Traceback (most recent call last):
  File "/Users/SB/Desktop/project/env/lib/python3.9/site-packages/asgiref/compatibility.py", line 33, in new_application
    instance = application(scope)
TypeError: 'module' object is not callable
127.0.0.1:55961 - - [04/Feb/2022:14:49:04] "WSDISCONNECT /ws/percentage/" - -

Here is my asgi.py

#djangoProject/asgi.py
import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import stream.routing

django_asgi_app = get_asgi_application()

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoProject.settings")
application = ProtocolTypeRouter({
  "http": django_asgi_app,
  "websocket": AuthMiddlewareStack(
        URLRouter(
            stream.routing.websocket_urlpatterns
        )
    ),
})

Here is the stream.routing:

# chat/routing.py
from django.urls import re_path

from .consumers import StreamConsumer, PercentageConsumer

websocket_urlpatterns = [
    re_path(r'ws/live/', StreamConsumer.as_asgi()),
    re_path(r'ws/percentage/', PercentageConsumer.as_asgi()),
]

Settings.py relevent code

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware'
]

WSGI_APPLICATION = 'djangoProject.wsgi.application'
ASGI_APPLICATION = 'djangoProject.asgi.application'

CELERY_BROKER_URL = 'redis://localhost:6379'

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}
carltongibson commented 2 years ago

So the application is asgi:application no?

sohambhardwaj commented 2 years ago

I am so sorry dude. Closing this.

KensingtonOscupant commented 1 year ago

Sorry to bring this up again, I haven't connected the dots yet. Where is the asgi:application supposed to go?

carltongibson commented 1 year ago

Does this not work: daphne -b 0.0.0.0 -p 8000 djangoProject.asgi:application ?

i.e. It's the application variable inside the djangoProject/asgi.py file that is the ASGI application that Daphne needs to run.

KensingtonOscupant commented 1 year ago

got it, thanks!