# phpjunior/mtube architecture

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

- Interactive diagram: https://gitdiagram.com/phpjunior/mtube
- Repository: https://github.com/phpjunior/mtube

## Overview

##### Main Components
- Frontend Client & User Interface:
- Represented by the Blade views, Livewire components, and static assets (found in resources/views, public, and resources/js/css).
- Separate areas include the user dashboard, channel pages, video watch pages, and profile management.
- Backend Application (Laravel):
- Encompasses the core Laravel MVC structure under the app directory: Controllers (both Frontend and Backend), Events, Jobs, Listeners, Models, and Middleware.
- Distinct administrative and user routes (see routes/backend versus routes/frontend and routes/web.php).
- Video Processing & Streaming:
- Video transcoding process using Laravel FFMpeg, the ConvertVideoForStreaming job, and the related configuration items.
- Channel and video management logic (models for Video, Channel, Comments, etc.) along with job processing (for example, StartConvert).
- Real-time & Broadcast Services:
- Laravel Websockets (via beyondcode/laravel-websockets) for real-time notifications, presence channels, and live subscription/view count updates.
- TusPHP for resumable uploads.
- External Servers and Integration Services:
- RTMP Server (rtmp/server.js) which handles live streaming.
- Uppy Companion Server (uppy/server.js) for handling uploads in conjunction with Uppy on the frontend.
- Porter which can start all servers together.
- Configuration & Infrastructure:
- Configuration files (in the config directory) for settings like mail, queue, and custom services (e.g., laravel-ffmpeg, rtmp).
- Environment variables (.env.example) and deployment scripts.

##### Relationships & Interactions
- User interactions via the Frontend Client trigger requests to the Laravel backend (via HTTP routes and Livewire components).
- The Backend handles video uploads and editing, triggering queued Jobs and Events. For example:
- A user uploads a video → TusPHP manages multipart uploads → Job (ConvertVideoForStreaming) is dispatched for transcoding.
- Real-time notifications and interactions (like live chat or view count updates) are managed by Laravel Websockets.
- Admin functionalities operate within a secured admin panel (both using backend controllers and Livewire components for admin modals and tables).
- Database interactions interface with all major modules including user authentication, video metadata, comments, settings (configured via migrations and models).
- External components (RTMP Server, Uppy Companion) have bidirectional communication with the backend:
- For example, the RTMP server receives streaming data and the backend (via events or calls) can process updates or manage channel states.

##### Architectural Patterns & Technologies to Highlight
- MVC & TALL Stack Pattern:
- The structure clearly follows Laravel’s MVC with an additional layer of Livewire components which provide the reactive frontend.
- Event-Driven & Asynchronous Processing:
- Integration of Third-Party Services:
- External integrations: TusPHP for uploads, FFmpeg for video processing, and Laravel Websockets for real-time data.
- Modular Components:
- Component boundaries (e.g., separation between frontend and backend, admin panel versus public channels).

##### Final Steps
- Break the system into layers like Presentation (Frontend UI), Application (Laravel controllers, Livewire components, events, and jobs), and Infrastructure (database, external servers).
- Double-check that every key piece derived from both the file tree and README is represented.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Presentation Layer"
        FE["Frontend Client & UI"]:::frontend
    end

    subgraph "Application Layer"
        BE["Backend Application (Laravel)"]:::backend
    end

    subgraph "Processing Layer"
        VP["Video Processing & Streaming"]:::video
        RT["Real-time & Broadcast Services"]:::realtime
    end

    subgraph "Infrastructure Layer"
        ES["External Servers & Integration Services"]:::external
        CONF["Configuration & Infrastructure"]:::config
        DB[(Database)]:::database
    end

    %% Connections
    FE -->|"HTTPRequest"| BE
    BE -->|"APICall"| DB
    BE -->|"QueueJob"| VP
    BE -->|"EventDispatch"| RT
    BE -->|"Integration"| ES
    CONF -->|"ConfiguresBackend"| BE
    CONF -->|"ConfiguresVideo"| VP
    CONF -->|"ConfiguresRealtime"| RT
    CONF -->|"ConfiguresExternal"| ES
    ES -->|"Feedback"| BE

    %% Click Events
    click FE "https://github.com/phpjunior/mtube/tree/master/resources/views/frontend"
    click BE "https://github.com/phpjunior/mtube/tree/master/app/"
    click VP "https://github.com/phpjunior/mtube/blob/master/app/Console/Commands/ConvertVideoForStreaming.php"
    click RT "https://github.com/phpjunior/mtube/blob/master/config/websockets.php"
    click ES "https://github.com/phpjunior/mtube/blob/master/rtmp/server.js"
    click CONF "https://github.com/phpjunior/mtube/tree/master/config/"

    %% Styles
    classDef frontend fill:#a6cee3,stroke:#1f78b4,stroke-width:2px;
    classDef backend fill:#b2df8a,stroke:#33a02c,stroke-width:2px;
    classDef video fill:#fdbf6f,stroke:#ff7f00,stroke-width:2px;
    classDef realtime fill:#cab2d6,stroke:#6a3d9a,stroke-width:2px;
    classDef external fill:#fb9a99,stroke:#e31a1c,stroke-width:2px;
    classDef config fill:#ffff99,stroke:#b15928,stroke-width:2px;
    classDef database fill:#d9d9d9,stroke:#828282;
```
