Creating a chat with Django Channels
Nowadays, when all sorts of chat rooms have become extremely popular, when every second large company has launched or developed its own instant messenger, when an increase of smiles and change in the text size is considered as innovation, in the era of iMessages, Slack, Hipchat, Messager, Google Allo, Zulip, etc. I will tell you how to keep up with the trend and write your own chat, using
We will take multichat as an example. This article can be considered as step-by-step instructions on creating such a chat.
This package allows our application to interact with a user not only using HTTP 1.1 (request-response), but also using HTTP/2 and WebSocket.
WebSocket is designed for exchanging messages between the client and the web server in real time. You should consider it as an open channel between the client and the server, with the ability to subscribe to the events sent to it.
Let’s define the main functions, which our chat app will have.
User authorization will be done on the basis of the built-in
django.contrib.auth. Django-channels support cookies-based user authentication, this is what we need.
Separate chat rooms with the possibility of adding, deleting and editing via standard Django admin control panel.
Message delivery in real time, which we will do using django-channels and websocket.
Installation and configuration of the environment
We will use
python 3.5.x, but
python 2.7 is also supported.