Deploy Django Channels 2.0 to Heroku

So you want to deploy Django Channels 2.0 to Heroku, hey? From previous posts, you should have guessed by now that I’m a fan of Heroku, especially for getting projects out the door quickly. Heroku is particularly fun to use for the following reasons

  1. You can have a pretty decent site setup for free
  2. It allows you understand how modern server apps are hosted/deployed – before you go down the path of platforms like docker, ceph etc.

So I had this particularly diverse project to work on over the past 2 weeks. Involved developing a an “agent” written in Go running on a GSM gateway that would communicate via websockets with an intermediary server and a vTiger CRM plugin. I wrote about it here – and hey, I’ve not forgotten about the part 2.

This intermediary platform was written in Python/Django, coupled with Channels for the websockets, Postgres for DB and Redis for the scheduler, queue, cache etc.

django-channels-heroku

django-channels-heroku

After several days of development and 2 days of testing, it was time to deploy to Heroku for the client to use. A google search on how to accomplish this turned up few results, with the most prominent being the blog entry by Heroku themselves written by oga pata pata himself Jacob. You should read the post, although you should be aware than quite a substantial bit of channels changed between v1 and v2. Other resources you will find helpful are

  1. http://channels.readthedocs.io/en/latest/deploying.html
  2. https://teamtreehouse.com/community/deploying-asgi-alongside-wsgi-on-heroku
  3. https://github.com/django/daphne

First, you need to understand that Django Channels is just another Django application, albeit one with a lot of moving parts itself. I will assume that you have already done quite some development in Channels so you understand routing (and where to put routing.py), comsumers etc, you understand what wsgi is and how asgi compares and you understand the purpose of daphne (and how it compares to gunicorn). Before deploying a Channels 2 app to Heroku, you need to pay attention to the following;

  1. Settings.py
    1. Add ASGI_APPLICATION = ‘my_project.routing.application’
    2. If using dj_database_url, do not set conn_max_age as per this issue.
    3. Make sure you set up CHANNELS_LAYERS. You should have by now.
  2. aspi.py – You’ll need to create this file, but Django should autogenerate it in the future.
    1. import os
      import django
      from channels.routing import get_default_application
      os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “my_project.settings”)
      django.setup()
      application=get_default_application()
  3. Procfile, runtime.txt as usuual. See the content of my Procfile below
    1. web: daphne my_project.asgi:application --port $port --bind 0.0.0.0
      worker: python runner.py
  4. My runner.py
    1. Now that your django site is running under asgi, it’s all “worker” based. This means that if you don’t fire up a worker, your site won’t get served. Of course, I’m assuming that you allow daphne handle both http and websocket connections. Really, runner.py is a script which allows me run multiple workers as subprocesses thereby saving even more $. So inside of it is the actual worker command we need. See below
      web: python manage.py runworker channels -v2
      The above command didn’t work for me without passing channels

Uhmm…what else…

There’s one more thing. I remembered dealing with an H10 “App crashed” error, but I can’t remember what the cause was or how I solved it. Uhmmm….

Aha!

So, I had thought (wondering what I was thinking) I could define the port i.e rather than pass $PORT and let heroku fill that in, I tried to define mine. So, don’t do that.

That’s all on deploying django channels 2.0 to Heroku. Hope you find this guide helpful.

Share and if you’ve got questions/corrections, drop a comment. If you’ve got a (tech) problem, we’re the crew to fix you up. #techSolutionsDevs

 

 

Leave a Reply