I've made an async chatroom app for a django website based on the excellent tutorial provided by django on django channels with daphne and redis.
https://channels.readthedocs.io/en/stable/tutorial/index.html
The app works very nicely in dev with redis running on a container in docker.
The challenge arises when I try to run it after deploying to Heroku!
I installed Redis Cloud using their free subscription. It's active and as far as I can see working fine. There are no databases connecting to it.
I've pinged the redis process successfully from the heroku.cli.
I have used standard django logic to allow it to connect to channels via a setting that points to a local .env variable for dev and the config var set up automatically by Redis Cloud for production.
REDIS_URL = os.getenv("REDISCLOUD_URL", "redis://localhost:6379")
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [REDIS_URL],
},
},
}
URLs are controlled as they are in the tutorial example from a simple routing file:
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r"^ws/chat/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()),
]
Of course, as soon as the user sends an async message, it fails, leaving a 404 message in the Heroku --tail that looks like this:
2025-02-16T19:51:41.383369+00:00 app[web.1]: 10.1.19.114 - - [16/Feb/2025:19:51:41 +0000] "GET /ws/chat/kitchen/ HTTP/1.1" 404 11248 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"
2025-02-16T19:51:41.383718+00:00 heroku[router]: at=info method=GET path="/ws/chat/kitchen/" host=sparksync-test-baedaeaf485c.herokuapp.com request_id=98f3ad28-61d1-4926-b125-ebc67fa78cb4 fwd="79.210.183.19" dyno=web.1 connect=0ms service=2ms status=404 bytes=11552 protocol=https
In dev, the app runs using the usual python manage.py runserver command, but wsgi hands over immediately to asgi to run the app.
The procfile line is the usual
web: gunicorn dating_app.wsgi:application
Could the issue that gunicorn doesn't hand over to daphne? Even though python manage.py runserver works fine in dev?
If this could be the cause my problem in the deployed environment, how can I fix it, given that the standard heroku daphne call shown above crashes immediately.
Even if you don't have any answers to these questions, any tips on troubleshooting would be gratefully accepted.