Real-Time Systems

Real-Time Secure Chat Application

Python

Real-Time Systems Featured

Real-Time Secure Chat Application

Authenticated multi-room messaging with Django Channels, Redis and PostgreSQL.

0 stars 0 forks 0 issues Created Nov 2026

Preview

Real-Time Secure Chat Application preview

Explore the README below or view the code on GitHub.

What this demonstrates

  • Session-authenticated WebSocket connections
  • Redis-backed room broadcasts
  • PostgreSQL message persistence
  • Origin checks and validated message input

Tech stack

Python 67.6%HTML 16.4%JavaScript 16.1%
DjangoDjango ChannelsWebSocketsRedisPostgreSQLDaphne

Overview

A multi-room chat application using Django Channels and Daphne for asynchronous WebSocket connections. Redis distributes room broadcasts, while PostgreSQL stores messages before delivery. Session authentication, origin checks and server-side message validation protect the messaging flow. The repository documents the implementation; runtime verification remains pending.

Project README

Multi-room Django chat

Step 1 foundation: Django 5.2, Channels with Daphne, PostgreSQL, and Redis. Step 2 adds custom users, rooms, messages, login/logout, and protected room pages. WebSocket handlers and live messaging follow in Steps 3 and 4.

Windows setup

Install Python 3.10+ first and ensure python is available in PowerShell. Then run from this directory:

python -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
Copy-Item .env.example .env

Edit .env with a random secret and your PostgreSQL credentials. The database and PostgreSQL role must already exist; the application does not create them. The default database and role names are both chat.

If Docker Desktop is installed, you can start local services with:

docker run --name chat-postgres -e POSTGRES_DB=chat -e POSTGRES_USER=chat -e POSTGRES_PASSWORD=replace-with-your-local-database-password -p 127.0.0.1:5432:5432 -v chat-postgres-data:/var/lib/postgresql/data -d postgres:16
docker run --name chat-redis -p 127.0.0.1:6379:6379 -d redis:7

Use the same database password in .env. For existing stopped containers, use docker start chat-postgres chat-redis.

Verify the foundation:

.\.venv\Scripts\python.exe manage.py check
.\.venv\Scripts\python.exe manage.py shell -c "from django.db import connection; connection.ensure_connection(); print('PostgreSQL connected')"
.\.venv\Scripts\python.exe manage.py shell -c "from asgiref.sync import async_to_sync; from channels.layers import get_channel_layer; layer = get_channel_layer(); channel = async_to_sync(layer.new_channel)(); async_to_sync(layer.send)(channel, {'type': 'health.check'}); print(async_to_sync(layer.receive)(channel))"

The custom user model is now configured. Generate its initial migration before applying any migrations: Run these commands, then create rooms and additional users at /admin/:

.\.venv\Scripts\python.exe manage.py makemigrations chat
.\.venv\Scripts\python.exe manage.py migrate
.\.venv\Scripts\python.exe manage.py createsuperuser
.\.venv\Scripts\python.exe manage.py runserver

These settings are for local development. Keep .env private.

Step 3: WebSocket API

Authenticated users can connect to /ws/rooms/<slug>/ using the Django session cookie. All rooms are available to every active authenticated user. Origins are checked against DJANGO_ALLOWED_HOSTS. Start Redis and PostgreSQL before testing.

Send {"message":"Hello"}. The server saves the message before broadcasting {"type":"message","id":1,"message":"Hello","username":"alice","date_added":"..."} to all connections in that room, including the sender. Invalid input returns {"type":"error","error":"..."} and is not stored. Messages must contain 1–2000 characters after trimming. Client-supplied usernames and room IDs are ignored.

Step 4 connects room pages to this endpoint with vanilla JavaScript, safe text rendering, a message form, and connection/error status. Reload after a disconnect to reconnect and load recent history. Runtime verification remains pending until Python, dependencies, and database services are available.

Manual end-to-end check

After setup, sign in and open the same room in two tabs. Send a message and confirm it appears once in both tabs and remains after reloading. Open a different room and verify it does not receive that message. Try text containing HTML tags; it should display literally. Stop Redis or the application server and confirm the page reports the connection problem; restart services and reload to reconnect.