Search code examples
djangoherokudjango-channelsdaphne

Daphne + Django Channels gives error 503 on Heroku


I've been trying to make my Django Channels app work on Heroku for the past 2 days but can't seem to make it work. I recently couldn't even run the app but after some digging I managed to start it up. The app works fine on localhost. I went from having error 404, to 500 and now 504. I've been trying to understand the Heroku logs but can't seem to find a solution. Hopefully someone can give me a hint on how to fix it. Here are the different files and logs.

Settings:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
    },
}

Requirements:

Django==3.0.7
gunicorn==19.9.0
pytz==2019.3
sqlparse==0.3.0
whitenoise==5.0.1
tweepy==3.8.0
dj-database-url==0.5.0
psycopg2-binary==2.8.5
discord.py==1.3.3
requests==2.23.0
channels-redis==2.4.2
channels==2.4.0
daphne==2.5.0

asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'APPNAME.settings')
django.setup()
application = get_default_application()

Procfile:

web: daphne APPNAME.asgi:application --port $PORT --bind 0.0.0.0 -v2

Heroku Logs:

2020-06-09T14:57:15.403840+00:00 app[web.1]: 2020-06-09 14:57:15,403 DEBUG    HTTP response complete for ['10.38.205.57', 33999]
2020-06-09T14:57:15.403926+00:00 app[web.1]: 10.38.205.57:33999 - - [09/Jun/2020:14:57:15] "GET /static/img/favicon.png" 200 10836
2020-06-09T14:57:15.400849+00:00 heroku[router]: at=info method=GET path="/static/img/favicon.png" host=www.domain.com request_id=fe8bd6bb-9050-4815-bf83-0b65cdb0b0cf fwd="90.90.83.191" dyno=web.1 connect=0ms service=7ms status=200 bytes=11090 protocol=https
2020-06-09T14:57:15.567171+00:00 app[web.1]: 2020-06-09 14:57:15,566 ERROR    [Failure instance: Traceback: <class 'ValueError'>: No route found for path 'ws/panel/order/96eda/'.
2020-06-09T14:57:15.567180+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/autobahn/websocket/protocol.py:2847:processHandshake
2020-06-09T14:57:15.567181+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/txaio/tx.py:366:as_future
2020-06-09T14:57:15.567182+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/twisted/internet/defer.py:151:maybeDeferred
2020-06-09T14:57:15.567182+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/daphne/ws_protocol.py:83:onConnect
2020-06-09T14:57:15.567183+00:00 app[web.1]: --- <exception caught here> ---
2020-06-09T14:57:15.567183+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/twisted/internet/defer.py:151:maybeDeferred
2020-06-09T14:57:15.567183+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/daphne/server.py:200:create_application
2020-06-09T14:57:15.567184+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/channels/routing.py:54:__call__
2020-06-09T14:57:15.567184+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/channels/sessions.py:47:__call__
2020-06-09T14:57:15.567185+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/channels/sessions.py:145:__call__
2020-06-09T14:57:15.567185+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/channels/sessions.py:169:__init__
2020-06-09T14:57:15.567185+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/channels/middleware.py:31:__call__
2020-06-09T14:57:15.567186+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/channels/routing.py:150:__call__
2020-06-09T14:57:15.567258+00:00 app[web.1]: ]
2020-06-09T14:57:15.567533+00:00 app[web.1]: 2020-06-09 14:57:15,567 DEBUG    Upgraded connection ['10.15.161.126', 32805] to WebSocket
2020-06-09T14:57:19.879740+00:00 app[web.1]: 2020-06-09 14:57:19,879 WARNING  dropping connection to peer tcp4:10.15.161.126:32805 with abort=True: WebSocket opening handshake timeout (peer did not finish the opening handshake in time)
2020-06-09T14:57:19.880267+00:00 app[web.1]: 2020-06-09 14:57:19,880 DEBUG    WebSocket closed for ['10.15.161.126', 32805]
2020-06-09T14:57:19.880376+00:00 app[web.1]: 10.15.161.126:32805 - - [09/Jun/2020:14:57:19] "WSDISCONNECT /ws/panel/order/96eda/" - -
2020-06-09T14:57:19.880503+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/ws/panel/order/96eda/" host=www.domain.com request_id=7b5b433b-c291-4994-8b5b-8e424447ce3e fwd="90.90.83.191" dyno=web.1 connect=0ms service=4319ms status=503 bytes=0 protocol=https
2020-06-09T14:56:44+00:00 app[heroku-redis]: source=REDIS addon=redis-cubed-69516 sample#active-connections=1 sample#load-avg-1m=0.165 sample#load-avg-5m=0.17 sample#load-avg-15m=0.155 sample#read-iops=0 sample#write-iops=0 sample#memory-total=15664256kB sample#memory-free=11266544kB sample#memory-cached=1033556kB sample#memory-redis=313968bytes sample#hit-rate=1 sample#evicted-keys=0

Thanks in advance.


Solution

  • Found my error, was the dumbest mistake. Error was in my routing, typed "wss/panel/order" thinking the wss part was my protocol.