A real-time chat application built with Django, Django Channels, and WebSockets. Supports public chat rooms, user profiles, and a friend system.
- Backend: Python 3.10+, Django 4.2, Django Channels 4.1, Daphne (ASGI)
- Database: PostgreSQL
- Real-time: WebSockets via Django Channels
- Channel layer: Redis (recommended for production) or in-memory (dev only)
- Media/uploads: Pillow
Install the following before starting:
| Tool | Version | Notes |
|---|---|---|
| Python | 3.10+ | python.org |
| PostgreSQL | 14+ | postgresql.org |
| Redis | 6+ | Optional — only needed for multi-process/production WebSocket support |
| pip | latest | Comes with Python |
git clone <your-repo-url>
cd MyChat# Create
python -m venv venv
# Activate — Windows
venv\Scripts\activate
# Activate — Linux/macOS
source venv/bin/activatepip install -r requirements.txtIf you are not using Redis,
channels_redisis still listed in requirements but won't be used unless you setREDIS_URL. It will install without issue.
Copy the example file and fill it in:
cp .env.example .envThen open .env and edit every value:
# Django
SECRET_KEY=some-long-random-string-here # generate with: python -c "from django.core.signing import get_cookie_signer; print(get_cookie_signer().key)"
DEBUG=True # set to False in production
ALLOWED_HOSTS=127.0.0.1 localhost
# PostgreSQL
DB_NAME=mychat
DB_USER=mychat_user
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432
# Email (you can use Gmail SMTP or leave blank during development)
EMAIL_FROM_USER=noreply@yourdomain.com
EMAIL_HOST=smtp.gmail.com
EMAIL_HOST_USER=your@gmail.com
EMAIL_HOST_PASSWORD=your_app_password
EMAIL_USE_TLS=True
EMAIL_PORT=587
# Redis (leave commented out for in-memory dev mode)
# REDIS_URL=redis://localhost:6379/0
LOG_LEVEL=DEBUGGenerating a SECRET_KEY — run this in your terminal:
python -c "import secrets; print(secrets.token_urlsafe(50))"
sudo apt install postgresql postgresql-contribPostgreSQL does not start automatically — you must start it manually each session:
sudo service postgresql startVerify it is running:
sudo service postgresql statusYou should see Active: active. If you want it to start automatically every time you open WSL, add this to your ~/.bashrc:
sudo service postgresql start > /dev/null 2>&1Connect as the PostgreSQL superuser:
sudo -u postgres psqlInside psql, run:
CREATE DATABASE mychat;
CREATE USER mychat_user WITH PASSWORD 'your_password';
ALTER ROLE mychat_user SET client_encoding TO 'utf8';
ALTER ROLE mychat_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE mychat_user SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE mychat TO mychat_user;
\qPostgreSQL 15 and later removed default write access to the public schema. You must grant it explicitly, connected to the mychat database specifically:
sudo -u postgres psql -d mychatInside psql:
GRANT ALL ON SCHEMA public TO mychat_user;
\qNote: This step is easy to miss. Without it,
python manage.py migratewill fail withpermission denied for schema public.
Check PostgreSQL is on the expected port:
sudo -u postgres psql -c "SHOW port;"Should output 5432 — matching DB_PORT in your .env.
Make sure the DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, and DB_PORT values in your .env match what you just created.
python manage.py migrateThis creates all tables including the custom user model (USERS.ChatUser), chat rooms, messages, and friend relationships.
python manage.py createsuperuserYou will be prompted for a username, email, and password. Use these credentials to log in to the Django admin at /admin/.
Note: The custom user model requires
first_name,last_name, andcreatesuperusercommand will prompt for all of them.
For development, Django serves static files automatically. For production:
python manage.py collectstaticThis project uses Daphne (ASGI server) to support WebSockets. Do not use runserver — it does not support WebSockets properly.
daphne -b 127.0.0.1 -p 8000 MyChat.asgi:applicationThe app will be available at: http://127.0.0.1:8000
Alternatively, during development Django's
runserverwill also work for basic testing (it has limited ASGI/WebSocket support in Django 4.2):python manage.py runserver
| URL | Description |
|---|---|
http://127.0.0.1:8000/ |
Home page |
http://127.0.0.1:8000/accounts/register/ |
Register a new account |
http://127.0.0.1:8000/accounts/login/ |
Log in |
http://127.0.0.1:8000/chat/ |
Unified chat dashboard |
http://127.0.0.1:8000/admin/ |
Django admin |
When a user registers, they receive an activation email. During development:
-
If you do not want to configure real SMTP, switch the email backend in
.envor directly insettings.pyto print emails to the console:# In MyChat/settings.py (temporary, dev only) EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Activation links will be printed to your terminal instead of sent.
For production or running multiple Daphne workers, set REDIS_URL in your .env:
REDIS_URL=redis://localhost:6379/0Start Redis:
# Linux/macOS
redis-server
# Windows (with WSL or Redis for Windows)
redis-serverWithout REDIS_URL, the app falls back to InMemoryChannelLayer which only works with a single process.
# After pulling new changes
pip install -r requirements.txt # in case new packages were added
python manage.py migrate # in case new migrations were added
daphne -b 127.0.0.1 -p 8000 MyChat.asgi:application