Skip to content

KSBMR/chatapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’¬ Django Chat App

A WhatsApp-style real-time chat application built with Django, Django Channels, and WebSockets.


✨ Features

  • πŸ” 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/

πŸ› οΈ Tech Stack

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)

πŸ“ Project Structure

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

πŸš€ Quick Start

1. Clone / Download the project

# If using git
git clone <your-repo-url>
cd django_chat_app

# Or just cd into the folder
cd django_chat_app

2. Create and activate a virtual environment

# Create virtual environment
python -m venv venv

# Activate it:
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activate

3. Install dependencies

pip install -r requirements.txt

4. Run database migrations

python manage.py migrate

5. Create a superuser (for admin panel)

python manage.py createsuperuser

6. Start the development server

python manage.py runserver

Important: Daphne (ASGI server) is configured automatically through Django's ASGI_APPLICATION setting and the daphne entry in INSTALLED_APPS. The standard runserver command will use Daphne for WebSocket support.

7. Open the app

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

πŸ”Œ How WebSocket Works

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

Key Components

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)

βš™οΈ Configuration Notes

Channel Layer (settings.py)

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)],
        },
    }
}

Production Checklist

  • 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

🎨 UI Highlights

  • 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

πŸ“ Database Models

Message

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

UserProfile

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

🧩 Testing the App

  1. Register two users (e.g., alice and bob)
  2. Open two browser tabs / windows (or use incognito for the second)
  3. Log in as alice in one tab, bob in the other
  4. Click on each other's name in the sidebar
  5. Type a message β€” it appears instantly in both windows! ⚑

πŸ“„ License

MIT β€” use freely for learning and personal projects.

About

Real-time chat application built with Django, featuring user authentication, messaging, and scalable backend architecture.

Topics

Resources

Stars

Watchers

Forks

Contributors