I'm trying to deploy Django Channels on Heroku using asgi alongside my existing wsgi implementation. Can I deploy both asgi and wsgi to heroku with the following setup?
My procfile:
web: gunicorn chatbot.wsgi --preload --log-file -
daphne: daphne chat.asgi:channel_layer --port $PORT --bind 0.0.0.0 -v2
chatworker: python manage.py runworker --settings=chat.settings -v2
My asgi.py file:
import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chat.settings")
channel_layer = get_channel_layer()
My wsgi.py file:
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chat.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
And my channel layers in settings.py:
CHANNEL_LAYERS = {
'default': {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')]
},
'ROUTING': 'chat.routing.channel_routing',
}
}
Like WSGI, ASGI describes a common interface between a Python web application and the web server. Unlike WSGI, ASGI allows multiple, asynchronous events per application. Plus, ASGI supports both sync and async apps.
As well as WSGI, Django also supports deploying on ASGI, the emerging Python standard for asynchronous web servers and applications.
Running ASGI alongside WSGI If that's the case, that's fine; you can run Daphne and a WSGI server alongside each other, and only have Daphne serve the requests you need it to (usually WebSocket and long-poll HTTP requests, as these do not fit into the WSGI model).
Uploading the Script Open the file using a text editor and add any dependencies needed such as numpy in order to run your project as when you deploy to Heroku the “pip” install command will be running to make sure all dependencies are present in order to run the script. 3. git add .
Figured this out, in case this might be relevant to anyone else. Using just asgi was the best solution. My procfile ended being:
web: daphne chat.asgi:channel_layer --port $PORT --bind 0.0.0.0 -v2
chatworker: python manage.py runworker --settings=chat.settings -v2
As a solution for serving static files, I updated my routing.py file to include a StaticFileConsumer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With