A WhatsApp-style real-time chat application built with Django, Django Channels, and WebSockets.
- π User Authentication β Register, Login, Logout
- π¬ Real-time Messaging β WebSocket-powered instant messaging
- π₯ User List β See all users, start a conversation instantly
- π Chat History β Persistent messages stored in SQLite
- π’ Online Status β See who's online in real time
- π Unread Badges β Unread message counts per conversation
- π€ User Profile β View your stats (sent/received messages)
- π¨ Modern Dark UI β Clean, responsive design with no Bootstrap dependency
- π οΈ Admin Panel β Manage users and messages at
/admin/
| Layer | Technology |
|---|---|
| Backend | Python 3.10+, Django 4.2 |
| Real-time | Django Channels 4, Daphne (ASGI) |
| WebSocket | channels.generic.websocket |
| Database | SQLite (default, zero config) |
| Frontend | Vanilla HTML, CSS, JavaScript |
| Fonts | Google Fonts (DM Sans + Syne) |
django_chat_app/
β
βββ django_chat_app/ # Project configuration package
β βββ __init__.py
β βββ asgi.py # ASGI entry point (HTTP + WebSocket routing)
β βββ settings.py # Django settings (Channels, DB, etc.)
β βββ urls.py # Root URL configuration
β βββ wsgi.py # WSGI entry (not used for real-time)
β
βββ chat/ # Main application
β βββ migrations/ # Database migrations
β βββ static/
β β βββ css/style.css # All styles (dark theme)
β β βββ js/chat.js # WebSocket client logic
β βββ templates/chat/
β β βββ login.html # Login page
β β βββ register.html # Registration page
β β βββ dashboard.html # User list / home page
β β βββ chat.html # One-to-one chat interface
β β βββ profile.html # User profile page
β βββ admin.py # Admin panel config
β βββ apps.py
β βββ consumers.py # WebSocket consumer (real-time logic)
β βββ forms.py # RegisterForm, LoginForm
β βββ models.py # Message, UserProfile models
β βββ routing.py # WebSocket URL routing
β βββ urls.py # HTTP URL patterns
β βββ views.py # All view functions
β
βββ manage.py
βββ requirements.txt
βββ README.md
# If using git
git clone <your-repo-url>
cd django_chat_app
# Or just cd into the folder
cd django_chat_app# Create virtual environment
python -m venv venv
# Activate it:
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activatepip install -r requirements.txtpython manage.py migratepython manage.py createsuperuserpython manage.py runserverImportant: Daphne (ASGI server) is configured automatically through Django's
ASGI_APPLICATIONsetting and thedaphneentry inINSTALLED_APPS. The standardrunservercommand will use Daphne for WebSocket support.
| URL | Purpose |
|---|---|
| http://127.0.0.1:8000/ | Dashboard (redirect) |
| http://127.0.0.1:8000/login/ | Login |
| http://127.0.0.1:8000/register/ | Register |
| http://127.0.0.1:8000/dashboard/ | User list |
| http://127.0.0.1:8000/chat/username/ | Chat with user |
| http://127.0.0.1:8000/admin/ | Admin panel |
Browser Django Server (Daphne ASGI)
β β
βββββ HTTP GET /chat/alice/ ββββββββββββΆβ (Django view renders chat.html)
βββββ HTML page with JS ββββββββββββββββ
β β
βββββ WS CONNECT /ws/chat/alice/ ββββββΆβ consumers.py β connect()
β β β’ Joins room group "chat_alice_bob"
β β β’ Marks user online
βββββ WS OPEN ββββββββββββββββββββββββββ
β β
βββββ WS SEND {"message":"Hello!"} ββββΆβ consumers.py β receive()
β β β’ Saves message to DB
β β β’ group_send to "chat_alice_bob"
β β
βββββ WS MESSAGE {"type":"chat_message",β consumers.py β chat_message()
β "message":"Hello!", β β’ Forwards event to WebSocket
β "sender":"bob", β
β "timestamp":"14:32"} ββββββββββββ
β β
β (JS appends bubble to chat window) β
β β
βββββ WS CLOSE βββββββββββββββββββββββββΆβ consumers.py β disconnect()
β β β’ Leaves room group
β β β’ Marks user offline
| File | Role |
|---|---|
asgi.py |
Routes HTTP β Django, WebSocket β Channels |
routing.py |
Maps /ws/chat/<username>/ to ChatConsumer |
consumers.py |
Handles connect / receive / disconnect events |
chat.js |
Opens WebSocket, sends messages, renders bubbles |
settings.py |
Configures CHANNEL_LAYERS (InMemory for dev) |
For development (default, zero config):
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer',
}
}For production (requires Redis):
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [('127.0.0.1', 6379)],
},
}
}- Set
DEBUG = False - Set a strong
SECRET_KEY(use environment variable) - Configure
ALLOWED_HOSTS - Switch to Redis channel layer
- Run
python manage.py collectstatic - Use a production database (PostgreSQL recommended)
- Serve with Daphne behind Nginx
- Dark luxury theme with emerald green accents
- Syne display font for headings
- DM Sans for body text
- Smooth message animations
- Real-time online/offline status dot
- Connection toast (connecting / connected / disconnected)
- Responsive sidebar with user search
- Unread message badges
| Field | Type | Description |
|---|---|---|
| sender | ForeignKey | User who sent the message |
| receiver | ForeignKey | User who received it |
| message | TextField | Message content |
| timestamp | DateTimeField | When it was sent |
| is_read | BooleanField | Whether it has been read |
| Field | Type | Description |
|---|---|---|
| user | OneToOneField | Link to Django's User model |
| is_online | BooleanField | Current online status |
| last_seen | DateTimeField | Last seen timestamp |
| avatar_color | CharField | Hex color for avatar |
- Register two users (e.g.,
aliceandbob) - Open two browser tabs / windows (or use incognito for the second)
- Log in as
alicein one tab,bobin the other - Click on each other's name in the sidebar
- Type a message β it appears instantly in both windows! β‘
MIT β use freely for learning and personal projects.