# ikraamdaanis/discourse architecture

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

- Interactive diagram: https://gitdiagram.com/ikraamdaanis/discourse
- Repository: https://github.com/ikraamdaanis/discourse

## Overview

##### Main Components

A. Client-Side (Next.js Application):
- Authentication Layer (Clerk)
- Real-time Communication Layer (WebSocket)
- Feature Modules:
- Chat System
- Server Management
- Channel Management
- Voice/Video Call System (LiveKit)
- User Profile Management

B. Server-Side:
- WebSocket Server
- API Routes
- Controllers Layer
- Database Layer (MySQL/PlanetScale)

##### Key Relationships to Highlight

a) Client-Server Communication:
- REST API calls
- WebSocket connections
- Real-time event handling

b) Data Flow:
- Authentication flow
- Message handling
- Server/Channel management
- Media handling (voice/video)

##### Specific Implementation Details

**Client Layer:**
**┌─────────────────────────────────────────────┐**
- UI Components (React/Next.js)               │
- Navigation                              │
- Chat Features                           │
- Server Management                       │
- Profile Management                      │
- ┘
- ↕️
**Middleware Layer:**
**┌─────────────────────────────────────────────┐**
- Authentication (Clerk)                   │
- API Routes                              │
- WebSocket Handlers                      │
- ┘
- ↕️
**Server Layer:**
**┌─────────────────────────────────────────────┐**
- Controllers                             │
- WebSocket Server                        │
- Database (Prisma/MySQL)                 │
- ┘

##### Important Elements

a) External Services Integration:
- Clerk Authentication
- LiveKit for voice/video
- UploadThing for file uploads
- PlanetScale database

b) Internal Systems:
- Server management system
- Channel management system
- Direct messaging system
- Real-time chat system
- Voice/Video call system

##### Data Flow Indicators

- User authentication flow
- Message flow (both real-time and REST)
- Media streaming direction
- Database queries and responses

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        UI[UI Components]
        Nav[Navigation]
        Chat[Chat Features]:::chat
        ServerMgmt[Server Management]:::server
        ProfileMgmt[Profile Management]:::profile
        UI --> Nav
        UI --> Chat
        UI --> ServerMgmt
        UI --> ProfileMgmt
    end

    subgraph Middleware
        Auth[Authentication]:::auth
        APIRoutes[API Routes]:::api
        WSHandlers[WebSocket Handlers]:::websocket
    end

    subgraph Server
        Controllers[Controllers]
        WSServer[WebSocket Server]:::websocket
        DB[(Database)]:::database
    end

    subgraph ExternalServices
        Clerk[Clerk Auth]:::external
        LiveKit[LiveKit]:::external
        UploadThing[UploadThing]:::external
        PlanetScale[PlanetScale]:::external
    end

    UI --> Auth
    Auth --> APIRoutes
    Auth --> WSHandlers
    APIRoutes --> Controllers
    WSHandlers --> WSServer
    Controllers --> DB
    WSServer --> DB

    Auth --> Clerk
    Chat --> LiveKit
    Chat --> UploadThing
    DB --> PlanetScale

    %% Click Events
    click Auth "https://github.com/ikraamdaanis/discourse/tree/main/client/app/(auth)/(routes)"
    click WSHandlers "https://github.com/ikraamdaanis/discourse/blob/main/client/components/providers/socket-provider.tsx"
    click Chat "https://github.com/ikraamdaanis/discourse/tree/main/client/features/chat"
    click ServerMgmt "https://github.com/ikraamdaanis/discourse/tree/main/client/features/server"
    click APIRoutes "https://github.com/ikraamdaanis/discourse/tree/main/client/app/api"
    click LiveKit "https://github.com/ikraamdaanis/discourse/blob/main/client/features/chat/components/LiveKit.tsx"
    click ProfileMgmt "https://github.com/ikraamdaanis/discourse/tree/main/client/features/profile"
    click DB "https://github.com/ikraamdaanis/discourse/blob/main/client/prisma/schema.prisma"

    %% Styling
    classDef auth fill:#90EE90
    classDef websocket fill:#FFD700
    classDef database fill:#DDA0DD
    classDef external fill:#FF6B6B
    classDef api fill:#87CEEB
    classDef chat fill:#ADD8E6
    classDef server fill:#98FB98
    classDef profile fill:#DEB887

    %% Legend
    subgraph Legend
        L1[Client Components]:::chat
        L2[Authentication]:::auth
        L3[WebSocket]:::websocket
        L4[Database]:::database
        L5[External Services]:::external
        L6[API Routes]:::api
    end
```
