Been scrolling though every question on StackOverflow and Youtube to find an answer. I'm deploying both WSGI and ASGI/websockets via Django channels on Heroku. It works locally, but it's been giving me trouble during production.
Setup
Here's my procfile:
web: daphne API.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python manage.py runworker background -v2
Settings.py:
# Redis Setup
ASGI_APPLICATION = 'API.routing.application'
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(HEROKU_URL, 6379)],
},
},
}
asgi.py:
import os
import django
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'API.settings')
django.setup()
application = get_asgi_application()
And finally, routing.py:
application = ProtocolTypeRouter({
'websocket':AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
[
url(r"^timer/$", TestConsumer.as_asgi()),
]
)
)
)
,'channel':ChannelNameRouter({
'background':BackgroundConsumer.as_asgi()
})
})
Problem/logs
Heroku logs when connecting to the websocket:
2021-07-02T19:42:42.209398+00:00 app[web.1]: IP - - [02/Jul/2021:19:42:42] "GET /chat_switch/1/" 200 10480
2021-07-02T19:43:02.708851+00:00 app[web.1]: IP - - [02/Jul/2021:19:43:02] "WSCONNECTING /timer/" - -
2021-07-02T19:43:02.709658+00:00 app[web.1]: 2021-07-02 19:43:02,709 DEBUG Upgraded connection [IP, 32183] to WebSocket
2021-07-02T19:43:03.058159+00:00 app[web.1]: 2021-07-02 19:43:03,057 ERROR Exception inside application: Django can only handle ASGI/HTTP connections, not websocket.
2021-07-02T19:43:03.058167+00:00 app[web.1]: Traceback (most recent call last):
2021-07-02T19:43:03.058168+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/asgi.py", line 143, in __call__
2021-07-02T19:43:03.058168+00:00 app[web.1]: raise ValueError(
2021-07-02T19:43:03.058169+00:00 app[web.1]: ValueError: Django can only handle ASGI/HTTP connections, not websocket.
2021-07-02T19:43:03.059813+00:00 app[web.1]: 2021-07-02 19:43:03,059 INFO failing WebSocket opening handshake ('Internal server error')
2021-07-02T19:43:03.060494+00:00 app[web.1]: 2021-07-02 19:43:03,060 WARNING dropping connection to peer tcp4:IP:32183 with abort=False: Internal server error
2021-07-02T19:43:03.064484+00:00 app[web.1]: 2021-07-02 19:43:03,063 DEBUG WebSocket closed for [IP, 32183]
Other than that, the regular API functions work as intended.
What I've tried
- Changed procfile to the answer from here: Websockets with Django Channels on Heroku (didn't work)
- Changed asgi.py to the answer from here: Django daphne asgi: Django can only handle ASGI/HTTP connections, not websocket (no difference in ouput)
- Tinkered with Redis URL in
settings.py
- Contemplating changing careers
If anyone knows what I can do from here, I'd appreciate it.