Thursday, September 23, 2021

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

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

El miércoles, 22 de septiembre de 2021 a las 20:37:50 UTC, mab.mo...@gmail.com escribió:
Hello, 

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 manage.py runserver ....
I am trying to deploy the project in production but cannot cat Daphne to start. Error messages and relevant files below. 

Questions
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 0.0.0.0 -p 8001 mabtest.asgi:main
Traceback (most recent call last):
  File "/home/marc/Env/mabtest/bin/daphne", line 8, in <module>
    sys.exit(CommandLineInterface.entrypoint())
  File "/home/marc/Env/mabtest/lib/python3.8/site-packages/daphne/cli.py", line 170, in entrypoint
    cls().run(sys.argv[1:])
  File "/home/marc/Env/mabtest/lib/python3.8/site-packages/daphne/cli.py", line 232, in run
    application = import_by_path(args.application)
  File "/home/marc/Env/mabtest/lib/python3.8/site-packages/daphne/utils.py", 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/asgi.py -------

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")
django.setup()
application = get_default_application()

----- main/routing.py -----

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(
            self.group_name,
            self.channel_name
        )

        await self.accept()

        # group creates a message in key value pair format 

        now = datetime.now()

        await self.channel_layer.group_send(
            self.group_name,
            {
                '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 = datetime.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(

            self.group_name,
            self.channel_name
        )

--
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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e8332c6f-9f67-48fc-887f-da89cdcc03d4n%40googlegroups.com.

No comments:

Post a Comment