Python Chat Tutorial with Django and React

By Nick Parsons

This tutorial will explain how to build a chat application with Python, Django and React.

Unlike other tutorials, I’m not using Python/Django for WebSocket connections. While doing so may seem cool from a tech perspective, it's pretty sluggish and expensive – especially if you have a halfway decent number of users. Languages such as C++, Go and Elixir are much better at handling the core of chat.

In this tutorial, we will use Stream, an API for chat that takes care of WebSocket connections and other heavy lifting using Go, Raft and RocksDB.

Table of Contents:

The GitHub repo for the code below can be found at

Let's code! 🤓

Step 1 – React Chat Demo UI

Before we start thinking about the Python chat side of things let's spin up a simple React frontend, so we have something nice and visual to look at:

$ yarn global add create-react-app
$ brew install node && brew install yarn # skip if installed
$ create-react-app chat-frontend
$ cd chat-frontend
$ yarn add stream-chat-react

Replace the code in src/App.js with:

import React from "react";
import { Chat, Channel, ChannelHeader, Thread, Window
} from "stream-chat-react";
import { MessageList, MessageInput } from "stream-chat-react";
import { StreamChat } from "stream-chat"; import "stream-chat-react/dist/css/index.css"; const chatClient = new StreamChat("qk4nn7rpcn75"); // Demo Stream Key
const userToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiY29vbC1za3ktOSJ9.mhikC6HPqPKoCP4aHHfuH9dFgPQ2Fth5QoRAfolJjC4"; // Demo Stream Token chatClient.setUser( { id: "cool-sky-9", name: "Cool sky", image: "" }, userToken
); const channel ="messaging", "godevs", { // add as many custom fields as you'd like image: "", name: "Talk about Go"
}); const App = () => ( <Chat client={chatClient} theme={"messaging light"}> <Channel channel={channel}> <Window> <ChannelHeader /> <MessageList /> <MessageInput /> </Window>
 <Thread /> </Channel>
); export default App;

Next, run yarn start to see the chat in action!

Step 2 - Django/Python Setup (skip if you already have it)

Make sure you have Python 3.7 up and running.

$ brew install python3 $ pip install virtualenv virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/
$ mkvirtualenv chatexample -p `which python3`
$ workon chatexample

If that does not work, please try this snippet:

$ python3 -m venv chatexample
$ source chatexample/bin/activate

Now that you’re in your virtual env you should see python 3 when you run:

To kick off a new Django project, use the following snippet:

$ pip install django
$ django-admin startproject mychat

And to start your app:

$ cd mychat
$ python runserver

Now, when you open http://localhost:8000, you should see this:


Step 3 - User Auth

As a next step lets setup Django’s user auth.

$ python migrate
$ python createsuperuser
$ python runserver

Visit http://localhost:8000/admin/ and login. Voila!

You should see the Django admin screen as shown below:

Django Admin

Step 4 - Django Rest Framework

One of my favorite packages for integrating react with Django is Django Rest Framework. To make everything work, we will need to create endpoints for:

We could build those ourselves; however, there is a package called Djoser that has already solved this problem. It configured the necessary API endpoints for user registration, login, password reset, etc.

To install Djoser, use the following snippet:

$ pip install djangorestframework djoser

Then, edit and change the file to contain:

from django.contrib import admin
from django.urls import path, include urlpatterns = [ path('admin/',, path('auth/', include('djoser.urls')), path('auth/', include('djoser.urls.authtoken')),

Once complete, edit and make the following changes:

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'rest_framework.authtoken', 'djoser',
] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', )

For more on the API endpoints that Djoser exposes, have a look at this:

Now, let’s go ahead and test the registration endpoint:

$ curl -X POST --data 'username=djoser&password=alpine12'

Step 5 - Generating Tokens to Access Stream's Chat Server

Now we need to customize the Djoser views to generate tokens for Stream. Let’s get started.

Let's organize our files a bit and create a chat app folder in our project (make sure that you are in the correct directory):

$ python startapp auth

Install stream-chat:

$ pip install stream-chat

Create a custom serializer in auth/ with the following logic:

from djoser.serializers import TokenSerializer
from rest_framework import serializers
from djoser.conf import settings as djoser_settings
from stream_chat import StreamChat
from django.conf import settings class StreamTokenSerializer(TokenSerializer): stream_token = serializers.SerializerMethodField() class Meta: model = djoser_settings.TOKEN_MODEL fields = ('auth_token','stream_token') def get_stream_token(self, obj): client = StreamChat(api_key=settings.STREAM_API_KEY, api_secret=settings.STREAM_API_SECRET) token = client.create_token( return token

And last, use the custom serializer by updating your file:

DJOSER = { 'SERIALIZERS': { 'token': 'auth.serializers.StreamTokenSerializer', }

Rerun your migration:

$ python migrate

To verify that it works, hit the login endpoint with a POST request:

$ curl -X POST --data 'username=djoser&password=alpine12'

Both the auth_token and stream_token should be returned.

Step 6 - Integrating Auth in React

Adding an auth later to the frontend is an essential step for obvious reasons. In our case, it’s especially useful because we can fetch a user token from the backend API (powered by Python) and dynamically use it when sending messages.

First, install the CORS middleware package for Django:

$ pip install django-cors-headers

Then, modify your to reference the djors-cors-header middleware:

INSTALLED_APPS = ( ... 'corsheaders', ...
) MIDDLEWARE = [ ... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ...

And finally, add the following to your file:


The next step requires a few modifications to be made to your frontend. To start, you will want to ensure that you have all of the dependencies installed via yarn:

$ yarn add axios react-dom react-router-dom

Next, create the following files within your src/ directory:

  • AuthedRoute.js
  • UnauthedRoute.js
  • withSession.js
  • Login.js
  • Chat.js


import React from "react";
import { BrowserRouter as Router, Switch } from "react-router-dom"; import Chat from "./Chat";
import Login from "./Login"; import UnauthedRoute from "./UnauthedRoute";
import AuthedRoute from "./AuthedRoute"; const App = () => ( <Router> <Switch> <UnauthedRoute path="/auth/login" component={Login} />
 <AuthedRoute path="/" component={Chat} />
); export default App;


import React from "react";
import { Redirect, Route } from "react-router-dom"; const AuthedRoute = ({ component: Component, loading, }) => { const isAuthed = Boolean(localStorage.getItem("token")); return ( <Route {} render={props => loading ? ( <p>Loading...</p>
 ) : isAuthed ? ( <Component history={props.history} {} />
 ) : ( <Redirect to={{ pathname: "/auth/login", state: { next: props.location } }} />
 ) } />
}; export default AuthedRoute;


import React from "react";
import { Redirect, Route } from "react-router-dom"; const AuthedRoute = ({ component: Component, loading, }) => { const isAuthed = Boolean(localStorage.getItem("token")); return ( <Route {} render={props => loading ? ( <p>Loading...</p>
 ) : !isAuthed ? ( <Component history={props.history} {} />
 ) : ( <Redirect to={{ pathname: "/" }} />
 ) } />
}; export default AuthedRoute;


import React from "react";
import { withRouter } from "react-router"; export default (Component, unAuthed = false) => { const WithSession = ({ user = {}, streamToken, ...props }) => || unAuthed ? ( <Component userId={} user={user} session={window.streamSession} {...props} />
 ) : ( <Component {...props} />
 ); return withRouter(WithSession);


import React, { Component } from "react";
import axios from "axios"; class Login extends Component { constructor(props) { super(props); this.state = { loading: false, email: "", password: "" }; this.initStream = this.initStream.bind(this); } async initStream() { await this.setState({ loading: true }); const base = "http://localhost:8000"; const formData = new FormData(); formData.set("username",; formData.set("password", this.state.password); const registration = await axios({ method: "POST", url: `${base}/auth/users/`, data: formData, config: { headers: { "Content-Type": "multipart/form-data" } } }); const authorization = await axios({ method: "POST", url: `${base}/auth/token/login/`, data: formData, config: { headers: { "Content-Type": "multipart/form-data" } } }); localStorage.setItem("token",; await this.setState({ loading: false }); this.props.history.push("/"); } handleChange = e => { this.setState({ []: }); }; render() { return ( <div className="login-root"> <div className="login-card"> <h4>Login</h4>
 <input type="text" placeholder="Email" name="email" onChange={e => this.handleChange(e)} />
 <input type="password" placeholder="Password" name="password" onChange={e => this.handleChange(e)} />
 <button onClick={this.initStream}>Submit</button>
 ); }
} export default Login;


import React, { Component } from "react";
import { Chat, Channel, ChannelHeader, Thread, Window
} from "stream-chat-react";
import { MessageList, MessageInput } from "stream-chat-react";
import { StreamChat } from "stream-chat"; import "stream-chat-react/dist/css/index.css"; class App extends Component { constructor(props) { super(props); this.client = new StreamChat("<YOUR_STREAM_APP_ID>"); this.client.setUser( { id: "cool-sky-9", name: "Cool Sky", image: "" }, localStorage.getItem("token") ); ="messaging", "godevs", { image: "", name: "Talk about Go" }); } render() { return ( <Chat client={this.client} theme={"messaging light"}> <Channel channel={}> <Window> <ChannelHeader /> <MessageList /> <MessageInput /> </Window>
 <Thread /> </Channel>
 ); }
} export default App;

Be sure to replace YOUR_STREAM_APP_ID with a valid Stream App ID which can be found on the dashboard at

Restart your frontend application and you should be hit with an auth wall! Enter your email and password and a token will be requested and stored in local storage.

Step 7 - Sending a Message from the Python chat server

Occasionally, you will want to write to the chat API using your backend Python-based server. Here’s a quick management command that you can use:

Verify that installed apps looks like this in

INSTALLED_APPS = [ 'corsheaders', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'rest_framework.authtoken', 'djoser',

Next, create the directory chat/management/commands. In that directory, add a file called with this content:

from import BaseCommand, CommandError
from django.conf import settings
from stream_chat import StreamChat class Command(BaseCommand): help = 'Broadcast the message on your channel' def add_arguments(self, parser): parser.add_argument('--message') def handle(self, *args, **options): client = StreamChat(api_key=settings.STREAM_API_KEY, api_secret=settings.STREAM_API_SECRET) client.update_user({"id": "system", "name": "The Server"}) channel ="messaging", "kung-fu") channel.create("system") response = channel.send_message({"text": "AMA about kung-fu"}, 'system') self.stdout.write('Successfully posted a message with id "%s"' % response['message']['id']))

You can try posting a message to the chat like this:

$ python broadcast --message hello

And you should see a response like this:


Final Thoughts

I hope you enjoyed this tutorial on building a chat application with Django, Python and React!

For an interactive tour of Stream Chat, please have a look at our API Tutorial on the Stream website. If you are interested in digging into the code for Stream Chat React Components, the full docs can be found here.

Happy coding! ✌