9

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',
    }
}
user2155400
  • 589
  • 5
  • 16

1 Answers1

14

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.

user2155400
  • 589
  • 5
  • 16
  • Any idea what `-v2` accomplishes? – Michael Hays Jan 27 '18 at 16:04
  • 1
    @MichaelHays `-v2` is for verbosity. Run `python manage.py runserver --help` for more info – Darkfish Jul 10 '18 at 00:48
  • When I deploy my app with your Procfile, I get this error `manage.py runworker: error: the following arguments are required: channels`. Do you have any idea what I need to do? – Anatol Mar 16 '20 at 13:24
  • 3
    @Anatol it might be so old but here is for those who do not know the answer `python manage.py runworker channels --settings=chat.settings -v2 ` – Shift 'n Tab Mar 28 '20 at 15:07
  • @Roel I am getting this error when I updated my Procfile worker to what you suggested: `ImproperlyConfigured: Cannot import ASGI_APPLICATION module 'chat.routing'`. I have a routing.py file in my chat project with an application. Do you know why I get this error? – Anatol Mar 28 '20 at 18:21
  • @Anatol This might be helpful https://stackoverflow.com/q/51634522/6143656 – Shift 'n Tab Mar 30 '20 at 09:34
  • Do I need to install daphne? What if I am not using channel layers? – Chymdy Mar 18 '22 at 13:06