# abhisheksrikanth/saturchat-backend architecture

> Architecture diagram of the GitHub repository abhisheksrikanth/saturchat-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhisheksrikanth/saturchat-backend
- Repository: https://github.com/abhisheksrikanth/saturchat-backend

## Overview

##### Project type and purpose
- This is a **real-time group chat backend** built with Django (REST + Channels)
- It provides WebSocket chat, JWT auth, async AI-powered assistants, profile management, and uses Docker for containerization.

##### File structure for architectural clues
- Top-level Django project: `saturchat/` (ASGI app, Celery config, settings, URL routing)
- Two main Django apps:
– `accounts/` handles user model, auth (JWT), middleware, management commands
– `chat/` handles chat models, serializers, views, WebSocket consumers, async tasks, AI provider integrations
- Async infrastructure:
– Celery tasks in `chat/tasks/`
– Redis for broker + channel layer (via Channels)
- Real-time layer:
– `chat/consumers/` with `chat_consumer.py`, `user_consumer.py`
– Django Channels routing in `chat/routing.py`
- Deployment/config:
– `Dockerfile`, `docker-compose.yml`, `start.sh`
– CI workflow in `.github/workflows/build.yml`

##### Extract key elements from README
- Web server: Daphne (ASGI) managed by Docker Compose behind NGINX proxy
- Persistent store: PostgreSQL
- Cache & broker: Redis
- Task queue & scheduler: Celery + Celery beat
- External AI services: OpenAI, Anthropic (Claude), Gemini
- Auth: JWT (access & refresh tokens)
- Containerized, CI/CD ready

##### Main components
1. Clients (Web/Mobile)
2. NGINX Reverse Proxy
3. ASGI Server (Daphne)
4. Django Application
- REST API (accounts, chat views)
- WebSocket handlers (ChatConsumer, UserConsumer)
5. Redis
- Channels layer (Pub/Sub for WebSockets)
- Celery broker + result backend
6. Celery Worker & Beat Scheduler
7. PostgreSQL Database
8. External Services (OpenAI, Anthropic, Gemini)

##### Relationships & data flows
- HTTP clients → NGINX → Daphne → Django REST Views → PostgreSQL
- WebSocket clients → NGINX → Daphne → Django Channels → Redis → ChatConsumer ↔ Database/Celery
- Django code enqueues tasks → Celery Worker (via Redis) → tasks call external AI APIs → results persisted in DB or pushed to clients
- Celery Beat triggers periodic cleanup tasks → Redis → Celery Worker

##### Architectural patterns & principles
- Monolithic Django app split into **apps** (accounts, chat)
- **Event-driven** & **pub/sub** for real-time via Channels + Redis
- **Async task queue** pattern with Celery for heavy/IO tasks (AI calls, cleanup)
- **12-factor** (env vars, logs to stdout, backing services as attached resources)
- **Containerized micro-services style** within a single Compose network

##### Final checklist
[ ] All eight main components are shown
– Blue for internal Django services
– Green for data stores
– Orange for external AI services

## Diagram (Mermaid)

```mermaid
flowchart TB
    Clients["Clients\n(Web/Mobile)"]:::external
    Clients -->|HTTP| NGINX
    Clients -->|WebSocket| NGINX

    subgraph "Docker Compose"
        direction TB
        NGINX["NGINX\n(Reverse Proxy)"]:::internal
        NGINX -->|HTTP/WS| Daphne

        Daphne["Daphne\n(ASGI Server)"]:::internal
        Daphne -->|ASGI| ASGI_Config
        Daphne -->|Routes| URLs
        Daphne -->|Invoke| Settings

        subgraph "Django Application"
            direction TB
            ASGI_Config["ASGI Config"]:::internal
            Settings["Settings"]:::internal
            URLs["URL Routing"]:::internal

            subgraph "Apps"
                direction TB
                Accounts["Accounts App"]:::internal
                Chat["Chat App"]:::internal
            end

            subgraph "Functionality"
                direction TB
                REST["Django REST API"]:::internal
                Channels["Django Channels"]:::internal
            end

            REST -->|CRUD| Postgres
            Channels -->|Pub/Sub| Redis
            Redis -->|Deliver| ChatConsumer
            ChatConsumer -->|Read/Write| Postgres
        end

        subgraph "Async Infrastructure"
            direction TB
            Redis["Redis\n(Broker & Channel Layer)"]:::datastore
            Postgres["PostgreSQL"]:::datastore
            CeleryConfig["Celery Config"]:::internal
            CeleryWorker["Celery Worker"]:::internal
            CeleryBeat["Celery Beat"]:::internal

            Chat -->|Enqueue| Redis
            REST -->|Enqueue| Redis
            CeleryBeat -->|Schedule| Redis
            Redis -->|Dispatch| CeleryWorker
            CeleryWorker -->|Results| Redis
            CeleryWorker -->|Persist| Postgres
            CeleryWorker -->|Push to Channels| Redis
        end
    end

    CeleryWorker -->|Calls| ExternalAPIs
    ExternalAPIs["External AI Services\n(OpenAI, Anthropic, Gemini)"]:::external
    ExternalAPIs -->|Respond| CeleryWorker

    %% Click Events
    click ASGI_Config "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/saturchat/asgi.py"
    click CeleryConfig "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/saturchat/celery.py"
    click Settings "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/saturchat/settings.py"
    click URLs "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/saturchat/urls.py"
    click Accounts "https://github.com/abhisheksrikanth/saturchat-backend/tree/main/accounts/"
    click Chat "https://github.com/abhisheksrikanth/saturchat-backend/tree/main/chat/"
    click Routing "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/routing.py"
    click ChatConsumer "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/consumers/chat_consumer.py"
    click UserConsumer "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/consumers/user_consumer.py"
    click AITasks "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/tasks/ai.py"
    click CleanupTasks "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/tasks/cleanup.py"
    click GeneralTasks "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/tasks/general.py"
    click OpenAI "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/llms/openai.py"
    click Anthropic "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/llms/anthropic.py"
    click Gemini "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/chat/llms/gemini.py"
    click Dockerfile "https://github.com/abhisheksrikanth/saturchat-backend/tree/main/Dockerfile"
    click Compose "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/docker-compose.yml"
    click Start "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/start.sh"
    click CI "https://github.com/abhisheksrikanth/saturchat-backend/blob/main/.github/workflows/build.yml"

    %% Styles
    classDef internal fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef datastore fill:#d4edda,stroke:#333,stroke-width:1px
    classDef external fill:#fff3cd,stroke:#333,stroke-width:1px
```
