Skip to content

This repository contains a real-time chat application built with Django Channels and Docker Compose. It includes a Django server, a PostgreSQL database, and a Redis instance to enable real-time communication.

Notifications You must be signed in to change notification settings

ericknavarro97/realtime-django-chat

Repository files navigation

Realtime Django Chat

A real-time chat application built with Django Channels, featuring WebSocket communication, user authentication, and message persistence.

Features

  • Real-time Messaging: Instant message delivery using Django Channels and WebSockets
  • User Authentication: Sign up, login, and logout functionality
  • Chat Rooms: Create and join multiple chat rooms with unique slugs
  • Message Persistence: Messages stored in Redis for session persistence
  • Rate Limiting: Prevents spam with configurable message limits (10 messages per minute, 1 second between messages)
  • Responsive UI: Clean, modern interface for seamless communication
  • Docker Support: Complete containerized setup with PostgreSQL and Redis

Tech Stack

  • Backend: Django 5.2.7, Django Channels
  • Database: PostgreSQL
  • Cache/Message Broker: Redis
  • WebSockets: Django Channels with Redis Channel Layer
  • Frontend: HTML, CSS, JavaScript
  • Containerization: Docker & Docker Compose

Requirements

  • Docker
  • Docker Compose

Quick Start

  1. Clone the repository:

    git clone https://github.com/ericknavarro97/realtime-django-chat.git
    cd realtime-django-chat
  2. Start the application:

    docker compose up --build
  3. Access the application: Open your browser and navigate to http://localhost:8000

Usage

  1. Sign Up: Create a new account or log in with existing credentials
  2. Create Chat Room: Enter a chat room name to create a new room
  3. Join Chat: Click on any existing chat room to join the conversation
  4. Start Chatting: Send messages that appear instantly to all participants

Architecture

Apps Structure

  • accounts: User authentication (signup, login, logout)
  • chats: Chat room management and real-time messaging
  • config: Django project settings and configuration
  • services: Redis service layer for message persistence
  • utilities: Common abstract models and utilities

Key Components

  • ChatConsumer: WebSocket consumer handling real-time message broadcasting
  • RedisService: Manages message storage and retrieval from Redis
  • Rate Limiting: Prevents abuse with configurable message limits
  • BaseAbstract: Common model base with UUID primary keys and timestamps

Development Commands

# Build and run containers
docker compose up --build

# Run in background
docker compose up -d

# Run tests
docker compose exec django python manage.py test

# Create superuser
docker compose exec django python manage.py createsuperuser

# Run migrations
docker compose exec django python manage.py migrate

# Collect static files
docker compose exec django python manage.py collectstatic

API Endpoints

  • GET/POST / - Chat room list and creation
  • GET /<chat_slug>/ - Individual chat room
  • POST /signup/ - User registration
  • POST /login/ - User login
  • POST /logout/ - User logout
  • WS /ws/<chat_name>/ - WebSocket endpoint for real-time messaging

Configuration

The application uses environment variables for configuration. Key settings include:

  • Database: PostgreSQL connection parameters
  • Redis: Host and port for caching and channel layer
  • Django: Secret key, debug mode, allowed hosts

Future Improvements

  • Implement chat deletion (admin only)
  • Add more comprehensive error handling and user feedback
  • Environment variables configuration with .env file
  • Improve cache cleanup mechanisms
  • Add user presence indicators
  • Implement private messaging
  • Add message history pagination
  • Email notifications for new messages
  • File/image sharing capabilities

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests: docker compose exec django python manage.py test
  5. Submit a pull request

License

This project is open source and available under the MIT License.

About

This repository contains a real-time chat application built with Django Channels and Docker Compose. It includes a Django server, a PostgreSQL database, and a Redis instance to enable real-time communication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published