Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

Django ASGI Server is not starting for setting up Django channels and websocket connection


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

# Topic - Real-time Data with Front End Example
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_receive(self, event):
# print('Message received from Client...', event['text'])
# for i in range(10):
# self.send({
# 'type':'websocket.send',
# 'text': json.dumps({"count":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_receive(self, event):
# print('Message received from Client', event['text'])
# for i in range(10):
# await self.send({
# 'type':'websocket.send',
# 'text': json.dumps({"count":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


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (1)


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


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(