# dosserpaul/discord-clone architecture

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

- Interactive diagram: https://gitdiagram.com/dosserpaul/discord-clone
- Repository: https://github.com/dosserpaul/discord-clone

## Overview

##### Main Components

Frontend Layer:
- Next.js Application (App Router)
- React Components
- Client-side State Management
- WebSocket Client

Backend Layer:
- Next.js API Routes
- WebSocket Server (Socket.IO)
- Authentication Layer
- File Upload Service (UploadThing)

Database Layer:
- Prisma ORM
- Database (likely PostgreSQL based on Prisma usage)

##### Key Architectural Patterns to Highlight

a) Route Structure:
- Invite System (/app/(invite))
- Main Application (/app/(main))
- Initial Setup (/app/(setup))
- API Routes (/app/api/*)

b) Real-time Communication:
- WebSocket connections for chat
- Socket event handlers
- Message synchronization

c) Component Organization:
- UI Components
- Modal System
- Navigation
- Server Management
- Chat Interface

**Client Layer**
- Pages/Routes
- Components
- Hooks/State Management

**API Layer**
- REST Endpoints
- WebSocket Server
- File Upload Service

**Data Layer**
- Prisma ORM
- Database

##### Important Relationships

- Client-Server Communication:
- REST API calls
- WebSocket connections
- File upload flows

- Data Flow:
- User interactions
- Real-time updates
- State management

##### Implementation Details

- Next.js App Router structure
- API route handlers
- Socket.IO integration
- Prisma models and relationships
- State management flow
- Modal system architecture
- Component hierarchy

##### External Services

Integrations with:
- UploadThing for file handling
- Authentication service
- Database service

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        Router["Next.js App Router"]
        Components["React Components"]
        StateManagement["Client State"]
        WSClient["WebSocket Client"]:::websocket
        
        subgraph Components
            UI["UI Components"]:::ui
            Nav["Navigation"]:::nav
            Chat["Chat Interface"]:::chat
            ServerMgmt["Server Management"]:::server
            Modals["Modal System"]:::modal
        end
    end

    subgraph API
        APIRoutes["API Routes"]:::api
        WSServer["WebSocket Server"]:::websocket
        Upload["UploadThing Service"]:::external
        Auth["Authentication"]:::auth
        
        subgraph Routes
            InviteAPI["Invite System"]:::api
            ServerAPI["Server API"]:::api
            ChannelAPI["Channel API"]:::api
            MessageAPI["Message API"]:::api
        end
    end

    subgraph Database
        Prisma["Prisma ORM"]:::db
        DB[(PostgreSQL)]:::db
    end

    %% Relationships
    Router --> Components
    Components --> StateManagement
    Components --> WSClient
    WSClient <--> WSServer
    Components --> APIRoutes
    APIRoutes --> Auth
    APIRoutes --> Upload
    APIRoutes --> Prisma
    WSServer --> Prisma
    Prisma --> DB

    %% Click Events
    click Router "https://github.com/DosserPaul/discord-clone/tree/main/app"
    click WSClient "https://github.com/DosserPaul/discord-clone/blob/main/components/providers/socket-provider.tsx"
    click WSServer "https://github.com/DosserPaul/discord-clone/blob/main/pages/api/socket/io.ts"
    click Upload "https://github.com/DosserPaul/discord-clone/tree/main/app/api/uploadthing"
    click Prisma "https://github.com/DosserPaul/discord-clone/blob/main/prisma/schema.prisma"
    click Auth "https://github.com/DosserPaul/discord-clone/blob/main/lib/current-profile.ts"
    click Chat "https://github.com/DosserPaul/discord-clone/tree/main/components/chat"
    click ServerMgmt "https://github.com/DosserPaul/discord-clone/tree/main/components/server"
    click Nav "https://github.com/DosserPaul/discord-clone/tree/main/components/navigation"
    click Modals "https://github.com/DosserPaul/discord-clone/tree/main/components/modals"
    click StateManagement "https://github.com/DosserPaul/discord-clone/blob/main/hooks/use-modal-store.ts"
    click APIRoutes "https://github.com/DosserPaul/discord-clone/tree/main/app/api"
    click InviteAPI "https://github.com/DosserPaul/discord-clone/tree/main/app/(invite)"
    click UI "https://github.com/DosserPaul/discord-clone/tree/main/components/ui"
    click MessageAPI "https://github.com/DosserPaul/discord-clone/tree/main/pages/api/socket/messages"
    click ServerAPI "https://github.com/DosserPaul/discord-clone/tree/main/app/api/servers"
    click ChannelAPI "https://github.com/DosserPaul/discord-clone/tree/main/app/api/channels"
    click DB "https://github.com/DosserPaul/discord-clone/blob/main/lib/db.ts"

    %% Styles
    classDef frontend fill:#2563eb,color:white
    classDef api fill:#16a34a,color:white
    classDef websocket fill:#9333ea,color:white
    classDef db fill:#ca8a04,color:white
    classDef external fill:#dc2626,color:white
    classDef auth fill:#475569,color:white
    classDef ui fill:#0891b2,color:white
    classDef nav fill:#4338ca,color:white
    classDef chat fill:#0d9488,color:white
    classDef server fill:#7c3aed,color:white
    classDef modal fill:#0369a1,color:white

    %% Legend
    subgraph Legend
        Frontend["Frontend"]:::frontend
        APIComponent["API"]:::api
        WebSocket["WebSocket"]:::websocket
        Database["Database"]:::db
        External["External Service"]:::external
        AuthComponent["Auth"]:::auth
    end
```
