# burakorkmez/realtime-spotify-clone architecture

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

- Interactive diagram: https://gitdiagram.com/burakorkmez/realtime-spotify-clone
- Repository: https://github.com/burakorkmez/realtime-spotify-clone

## Overview

##### Main Components

A. Frontend Layer (React/TypeScript):
- User Interface Components
- Main Layout (MainLayout.tsx)
- Music Player (AudioPlayer.tsx)
- Authentication Components
- Admin Dashboard
- Chat Interface
- State Management (Stores)
- Authentication Store
- Music Store
- Player Store
- Chat Store

B. Backend Layer (Node.js):
- API Controllers
- Auth Controller
- Album Controller
- Song Controller
- User Controller
- Admin Controller
- Stats Controller
- Models Layer
- User Model
- Album Model
- Song Model
- Message Model

C. External Services:
- Clerk (Authentication)
- Cloudinary (Media Storage)
- MongoDB (Database)
- WebSocket Server (Real-time Features)

**┌─────────────────────────────────────────────────────────────┐**
- Client Layer                            │
- ┬────────────┬────────────────┬─────────────────┤
- UI/Pages  │   Stores   │ Audio Player   │  Real-time Chat │
- ┬──────┴─────┬──────┴───────┬────────┴────────┬────────┘

- ▼            ▼              ▼                  ▼
**┌─────────────────────────────────────────────────────────────┐**
- API Gateway                             │
- ┬───────────────────────────────┘

**┌─────────────────┬──────────┴──────────┬───────────────────┐**
- Auth Service   │  Business Logic     │   WebSocket Server │
- (Clerk)      │     Layer          │                    │
- ┬────────┴──────────┬─────────┴──────────┬─────────┘

**┌────────┴────────┬──────────┴─────────┬─────────┴──────────┐**
- MongoDB      │     Cloudinary     │     Real-time      │
- Database      │  (Media Storage)   │    Communication   │
- ┴──────────────────┴────────────────────┘

##### Important Flows to Highlight

a. Authentication Flow:
- Client → Clerk → Backend → Database

b. Music Streaming Flow:
- Client → Backend → Cloudinary → Client
- Audio Player → WebSocket → Other Clients (for real-time status)

c. Chat Flow:
- Client → WebSocket Server → Database → Other Clients

##### Additional Details

- Notes about scalability considerations
- Security boundaries and authentication checkpoints
- Where environment variables are used

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef external fill:#f1c40f,stroke:#f39c12,color:black
    classDef database fill:#9b59b6,stroke:#8e44ad,color:white
    classDef realtime fill:#e74c3c,stroke:#c0392b,color:white

    %% Frontend Layer
    subgraph Frontend
        UI[UI Components]
        Stores[State Management]
        Player[Audio Player]
        Chat[Chat Interface]
    end
    
    %% UI Components
    subgraph UIComponents
        MainLayout[Main Layout]
        AdminDash[Admin Dashboard]
        AuthUI[Authentication UI]
    end
    
    %% Stores
    subgraph StateManagement
        AuthStore[Auth Store]
        MusicStore[Music Store]
        PlayerStore[Player Store]
        ChatStore[Chat Store]
    end

    %% Backend Layer
    subgraph Backend
        APIGateway[API Gateway]
        Controllers[Controllers Layer]
        Models[Models Layer]
    end

    %% Controllers
    subgraph ControllerLayer
        AuthController[Auth Controller]
        AlbumController[Album Controller]
        SongController[Song Controller]
        UserController[User Controller]
        AdminController[Admin Controller]
        StatsController[Stats Controller]
    end

    %% Models
    subgraph ModelLayer
        UserModel[User Model]
        AlbumModel[Album Model]
        SongModel[Song Model]
        MessageModel[Message Model]
    end

    %% External Services
    Clerk[Clerk Authentication]
    Cloudinary[Cloudinary Storage]
    MongoDB[(MongoDB Database)]
    WebSocket[WebSocket Server]

    %% Relationships
    UI --> APIGateway
    Stores --> APIGateway
    Player --> WebSocket
    Chat --> WebSocket
    APIGateway --> Controllers
    Controllers --> Models
    Models --> MongoDB
    Controllers --> Cloudinary
    Controllers --> WebSocket
    AuthController --> Clerk

    %% Click Events
    click MainLayout "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/layout/MainLayout.tsx"
    click Player "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/layout/components/AudioPlayer.tsx"
    click AdminDash "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/pages/admin/AdminPage.tsx"
    click Chat "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/pages/chat/ChatPage.tsx"
    click AuthUI "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/components/SignInOAuthButtons.tsx"
    click AuthStore "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/stores/useAuthStore.ts"
    click MusicStore "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/stores/useMusicStore.ts"
    click PlayerStore "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/stores/usePlayerStore.ts"
    click ChatStore "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/frontend/src/stores/useChatStore.ts"
    click AuthController "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/controller/auth.controller.js"
    click AlbumController "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/controller/album.controller.js"
    click SongController "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/controller/song.controller.js"
    click UserController "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/controller/user.controller.js"
    click AdminController "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/controller/admin.controller.js"
    click StatsController "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/controller/stat.controller.js"
    click UserModel "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/models/user.model.js"
    click AlbumModel "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/models/album.model.js"
    click SongModel "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/models/song.model.js"
    click MessageModel "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/models/message.model.js"
    click Cloudinary "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/lib/cloudinary.js"
    click MongoDB "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/lib/db.js"
    click WebSocket "https://github.com/burakorkmez/realtime-spotify-clone/blob/master/backend/src/lib/socket.js"

    %% Style Applications
    class UI,MainLayout,AdminDash,AuthUI,Stores,Player,Chat frontend
    class APIGateway,Controllers,Models,AuthController,AlbumController,SongController,UserController,AdminController,StatsController backend
    class Clerk,Cloudinary external
    class MongoDB database
    class WebSocket realtime
```
