# soehlert/tunebox architecture

> Architecture diagram of the GitHub repository soehlert/tunebox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/soehlert/tunebox
- Repository: https://github.com/soehlert/tunebox

## Overview

##### Main Components

a. Client Layer:
- React Frontend (Browser Clients)
- Multiple client connections possible
- WebSocket connections

b. Server Layer:
- FastAPI Backend
- WebSocket Server
- API Routes (/music endpoints)

c. Integration Layer:
- Plex Server Connection
- Plex Client (Remote Player)

d. Data Layer:
- Redis Cache/Queue Management

**[Web Clients (React)] ←→ [Load Balancer/Nginx]**
- ↓
**[FastAPI Backend]**
- ↙     ↓     ↘
**[Redis] [Plex API] [WebSocket Server]**
- ↓
- [Plex Client]

##### Key Flows to Highlight

a. Music Library Access Flow:
**Web Client → FastAPI → Plex Server → Redis Cache**

b. Playback Control Flow:
**Web Client → WebSocket → FastAPI → Plex Client**

c. Queue Management Flow:
**Web Client → FastAPI → Redis → WebSocket → All Connected Clients**

##### Component Details

Frontend (React):
- Components: ArtistList, ArtistAlbums, TrackList, Queue, MusicControls
- WebSocket connection for real-time updates
- REST API client for library management

Backend (FastAPI):
- Music router endpoints
- WebSocket handler
- Plex service integration
- Redis service for queue management

Data Store (Redis):
- Queue storage
- State management
- Caching layer

External Services:
- Plex Server connection
- Plex Client for playback

##### Technical Details to Note

- Port mappings (80, 8000, 6379)
- API endpoints
- WebSocket connections
- Redis connections
- Plex integration points

##### The WebSocket-based synchronization mechanism

For this specific application, focus on showing how the real-time updates flow through the system and how the various components work together to provide a synchronized music playback experience across multiple clients.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Clients
        C1[Browser Client 1]
        C2[Browser Client 2]
        C3[Browser Client 3]
    end

    subgraph Frontend["Frontend Components"]
        AL["ArtistList"]:::frontend
        AA["ArtistAlbums"]:::frontend
        TL["TrackList"]:::frontend
        Q["Queue"]:::frontend
        MC["MusicControls"]:::frontend
        API["API Client"]:::frontend
    end

    subgraph LoadBalancer["Load Balancer"]
        NX["Nginx Proxy"]:::proxy
    end

    subgraph Backend["Backend Services"]
        FA["FastAPI Application"]:::backend
        WS["WebSocket Handler"]:::websocket
        MR["Music Router"]:::backend
        CF["Configuration"]:::config
    end

    subgraph Integration["Integration Layer"]
        PS["Plex Service"]:::integration
        RS["Redis Service"]:::integration
        RC["Redis Client"]:::integration
    end

    subgraph External["External Services"]
        PX["Plex Server"]:::external
        PC["Plex Client"]:::external
        RD[(Redis DB)]:::database
    end

    %% Client Connections
    C1 --> NX
    C2 --> NX
    C3 --> NX

    %% Frontend to Backend
    NX --> Frontend
    Frontend --> NX
    NX --> FA
    NX --> WS

    %% Frontend Component Relations
    AL --> API
    AA --> API
    TL --> API
    Q --> API
    MC --> API

    %% Backend Relations
    FA --> MR
    FA --> WS
    MR --> PS
    MR --> RS
    RS --> RC
    RC --> RD

    %% External Relations
    PS --> PX
    PS --> PC

    %% Click Events
    click AL "https://github.com/soehlert/tunebox/blob/main/frontend/src/components/ArtistList.tsx"
    click AA "https://github.com/soehlert/tunebox/blob/main/frontend/src/components/ArtistAlbums.tsx"
    click TL "https://github.com/soehlert/tunebox/blob/main/frontend/src/components/TrackList.tsx"
    click Q "https://github.com/soehlert/tunebox/blob/main/frontend/src/components/Queue.tsx"
    click MC "https://github.com/soehlert/tunebox/blob/main/frontend/src/components/MusicControls.tsx"
    click API "https://github.com/soehlert/tunebox/blob/main/frontend/src/api/axiosInstance.ts"
    click FA "https://github.com/soehlert/tunebox/blob/main/backend/main.py"
    click MR "https://github.com/soehlert/tunebox/blob/main/backend/routers/music.py"
    click WS "https://github.com/soehlert/tunebox/blob/main/backend/websockets.py"
    click CF "https://github.com/soehlert/tunebox/blob/main/backend/config.py"
    click PS "https://github.com/soehlert/tunebox/blob/main/backend/services/plex.py"
    click RS "https://github.com/soehlert/tunebox/blob/main/backend/services/redis.py"
    click RC "https://github.com/soehlert/tunebox/blob/main/backend/services/redis_client.py"
    click NX "https://github.com/soehlert/tunebox/blob/main/Compose/frontend/nginx.conf"

    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef websocket fill:#f1c40f,stroke:#f39c12,color:black
    classDef integration fill:#9b59b6,stroke:#8e44ad,color:white
    classDef external fill:#e74c3c,stroke:#c0392b,color:white
    classDef database fill:#e67e22,stroke:#d35400,color:white
    classDef config fill:#1abc9c,stroke:#16a085,color:white
    classDef proxy fill:#95a5a6,stroke:#7f8c8d,color:white
```
