Thursday, September 23, 2021

Re: Cannot start Daphne / Looking for channels production deployment with daphne/nginx

You are missing a main attribute on your file. Try with daphne -b -p 8001 mabtest.asgi:application

El miércoles, 22 de septiembre de 2021 a las 20:37:50 UTC, escribió:

I am attempting to add asgi/channels to an existing django project/web app. I was originally able to get channels to work via python runserver ....
I am trying to deploy the project in production but cannot cat Daphne to start. Error messages and relevant files below. 

1. Can someone provide direction on getting the daphne error corrected?
2. Does anyone have any instruction on getting channels to work with daphne and nginx?

Thank you.
Attempting to run daphne server with error ...

(mabtest) marc@mymachine:~/webdev/mabtest$ daphne -b -p 8001 mabtest.asgi:main
Traceback (most recent call last):
  File "/home/marc/Env/mabtest/bin/daphne", line 8, in <module>
  File "/home/marc/Env/mabtest/lib/python3.8/site-packages/daphne/", line 170, in entrypoint
  File "/home/marc/Env/mabtest/lib/python3.8/site-packages/daphne/", line 232, in run
    application = import_by_path(args.application)
  File "/home/marc/Env/mabtest/lib/python3.8/site-packages/daphne/", line 14, in import_by_path
    target = getattr(target, bit)
AttributeError: module 'mabtest.asgi' has no attribute 'main'

## NOTES ##
- project name: mabtest
- app name: main
- redis is installed

----- mabtest/ -------

from .wsgi import * 
import os
import django
from django.core.asgi import get_asgi_application
from channels.routing import get_default_application
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.conf.urls import url
from main.routing import ws_urlpatterns

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mabtest.settings")
application = get_default_application()

----- main/ -----

from django.urls import path
from .consumers import ws_consumer

# Set the path to call the consumer
ws_urlpatterns = [
    path('ws/main/', ws_consumer.as_asgi())


import json
#from channels.generic.websocket import WebsocketConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
from datetime import datetime
from time import sleep

# Define the consumer class to send the data through WebsocketConsumer
class ws_consumer(AsyncWebsocketConsumer):

    # make a web socket connection 

    async def connect(self):

        # make a channel layer and group so it can display on multiple browers tabs

        self.group_name = "aprs"

        await self.channel_layer.group_add(

        await self.accept()

        # group creates a message in key value pair format 

        now =

        await self.channel_layer.group_send(
                'type': 'my_messages', # must match function def name below
                'my_message_1': 'This is message #1',
                'my_message_2': 'This is message #2',
                'my_message_3': now.strftime("%H:%M:%S"),


    # receive message from group and send it through websocket 

    async def my_messages(self, event):

        # collect information 

        my_message_1 = event['my_message_1']
        my_message_2 = event['my_message_2']
        my_message_3 = event['my_message_3']

        # send information via json and websocket

        await self.send(text_data=json.dumps({
            'my_message_1': my_message_1,
            'my_message_2': my_message_2,
            'my_message_3': my_message_3,


#        self.accept()

#        while(True):

#            now =
#            self.send(json.dumps({'timeValue': now.strftime("%H:%M:%S")}))
#            sleep(1)

    # disconnects from websocket

    async def disconnect(self, close_code):

        await self.channel_layer.group_discard(


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment