hhaefliger
hhaefliger

Reputation: 531

Deploying django channels on heroku

I have created a standard django application with startproject, startapp, etc. and I want to deploy it on heroku. When I was using gunicorn I solved the directory issue like so:

web: gunicorn --pythonpath enigma enigma.wsgi

with the --pythonpath option. But now I am using django channels and so it is daphne. Is there an equivalent? I have tried everything but for the life of me I can't get the project to start. I always get issues with the settings file, apps not loaded or another assortment of cwd-related issues.

As given in the Heroku Django channels tutorial, I have tried:

daphne enigma.asgi:channel_layer --port 8888

This led to a variety of module not found errors with asgi and settings.

I also tried

daphne enigma.enigma.asgi:channel_layer --port 8888

This led to module not found enigma.settings errors.

I also tried

cd enigma && daphne enigma.asgi:channel_layer --port 8888

Which led to Django apps not ready errors.

I also tried moving the Procfile and pipfiles into the project directory and deploying that subdirectory but once again I got apps not ready errors.

I have now started temporarily using

cd enigma && python manage.py runserver 0.0.0.0:$PORT

But I know that you're not supposed to do this in production.

Upvotes: 1

Views: 633

Answers (1)

Ajay Lingayat
Ajay Lingayat

Reputation: 1673

Try this:

Procfile

web: daphne enigma.asgi:application --port $PORT --bind 0.0.0.0 -v2
chatworker: python manage.py runworker --settings=enigma.settings -v2

settings.py

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

asgi.py

import os, django
from channels.routing import get_default_application

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

Upvotes: 2

Related Questions