In the settings.py ASGI_APPLICATION = '_keenthemes.asgi.application' usually ASGI_APPLICATION = 'appname.asgi.application'
But app name is inconsistent
In the installed apps section app name is given as dashboards.apps.DashboardsConfig by folder hierarchy i can consider dashboards as app name
so possible values for ASGI_APPLICATION are ASGI_APPLICATION = 'dashboards.apps.DashboardsConfig.asgi.application' ASGI_APPLICATION = '_keenthemes.asgi.application' ASGI_APPLICATION = 'dashboards.asgi.application'
Which is correct In asgi.py
""" ASGI config for _keenthemes project.
It exposes the ASGI callable as a module-level variable named application.
For more information on this file, see https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ """
import os from django.core.asgi import get_asgi_application from channels.routing import ProtocolTypeRouter, URLRouter import dashboards.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '_keenthemes.settings')
application = ProtocolTypeRouter({ 'http': get_asgi_application(), 'websocket': URLRouter( dashboards.routing.websocket_urlpatterns ) })
At routings.py
from django.urls import path from . import consumers
websocket_urlpatterns = [ path('ws/sc/', consumers.MySyncConsumer.as_asgi()), path('ws/ac/', consumers.MyAsyncConsumer.as_asgi()), ]
consumer.py
from channels.consumer import SyncConsumer, AsyncConsumer from channels.exceptions import StopConsumer from time import sleep import asyncio import json
class MySyncConsumer(SyncConsumer): def websocket_connect(self, event): print('Websocket Connected...', event) self.send({ 'type':'websocket.accept', })
def websocket_receive(self, event): print('Message received from Client...', event['text']) for i in range(10): self.send({ 'type':'websocket.send', 'text': str(i) }) sleep(1)
def websocket_disconnect(self, event): print('Websocket Disconnected...', event) raise StopConsumer()
class MyAsyncConsumer(AsyncConsumer): async def websocket_connect(self, event): print('Websocket Connected...', event) await self.send({ 'type':'websocket.accept', })
async def websocket_receive(self, event): print('Message received from Client', event['text']) for i in range(10): await self.send({ 'type':'websocket.send', 'text': str(i) }) await asyncio.sleep(1)
async def websocket_disconnect(self, event): print('Websocket Disconnected...', event) raise StopConsumer()
I think I have configured all the files correctly Please let me know the right app name Any insights will be of huge help Thanks
Hi Rohith,
Based on the code, I think the correct value for ASGI_APPLICATION is _keenthemes.asgi.application . Could you please try it?
If you have any more questions or encounter any further problems, please don't hesitate to ask. We're here to assist you!
Thanks