Hosting Django Chat App on Heroku with Redis & Daphne.

Collins Hillary
4 min readNov 17, 2021

(sighs) Finally!!!, I have been running away from creating a chat application from scratch for a long time but I decided to take up the challenge and Voila! I made it guys.

Just to note building a chat application using any language is not an easy task, especially if it’s your first time and so I decided to take this time to help you create and deploy a Django based chat application easily and stress-free ( of course I already underwent the stressful part for you).

Introduction

For this article, I will be mainly showing how to deploy the chat application and this entails the following:-

  1. Necessary Settings on the Django Chat application.
  2. Heroku environment setup.
  3. Modules and Packages involved.
  4. How to smile after going live.

Getting Started & Assumptions

As we start, I will be making a few assumptions about this project.

  1. You already have a Django project setup.
  2. You are well familiar with python and the Django infrastructure.
  3. You are familiar with the Heroku platform.

If you have a green mark on all of the above then kindly let’s proceed with the main cake below.

Kindly ensure you have the following packages installed

  1. Django Channels
  2. Channel-Redis
  3. Redis-server

I do have a Django project by the name of HilChat. This project entails one application called Chat and the main Hilchat folder that uses the following directory structure.

Hilchat Application Structure
Chat Application Structure

Now from the images above, we can clearly see there are a couple of files under each directory. We will be highlighting the main files under the main Hilchat application.

Hilchat Application Directory

We have the following main files for this article:-

  1. Settings.py

We all know the use of the settings.py file, this is where our main application settings/configurations are.

Modify your INSTALLED APPS section and add the channels and the chat application as below.

Chat applications utilize channels or rather a web socket to communicate to each other or to different rooms. For use to achieve this, we will need to utilize ASGI (Asynchronous Server Gateway Interface) on top of the existing WSGI(Web Server Gateway Interface). We will also need to connect to a Redis server instance.

Add the following to set up ASGI and create a connection to the Redis server instance.

The code above will tell Django the location of our ASGI settings, which is the asgi.py file, and also point Django to our Redis instance.

Notice the “REDIS_URL” string. This is set up on Heroku as an environment variable when we install the heroku-redis addon on Heroku.

2. asgi.py

asgi.py file

On our asgi.py file, we are just defining the protocols and pointers to our chat application. Modify your file to look like the above.

Amazingly that’s it for the Hilchat application modifications. Our application is now ready to be moved to the Heroku environment.

Heroku Setup

  1. Procfile

We would need to modify the procfile so as to use the daphne package with our asgi setup as below. Modify your procfile to look as below.

Login to Heroku, the application that you have created, and add the following line on the environment variables.

You are now done and ready to deploy and use the chat application you created easily.

I believe that was way easier, with no bugs. You can follow this link to do a complete Django chat application before proceeding with this tutorial.

In case of any queries or clarification feel free to reach out.

Happy Coding.

Author: Collins Munene

Email: collinshillary1@gmail.com

--

--

Collins Hillary

Software and Immersive Tech Developer and Security Analyst