# cb7chaitanya/streamly architecture

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

- Interactive diagram: https://gitdiagram.com/cb7chaitanya/streamly
- Repository: https://github.com/cb7chaitanya/streamly

## Overview

##### Project type and purpose
- This is a full-stack, microservices-style monorepo (Turborepo + pnpm) implementing a live video-streaming platform.
- Key user flows: user authentication and profile management, real-time media routing via an SFU, on-the-fly stream mixing/encoding, and optional live broadcasting (e.g. to YouTube).

##### High-level file-structure analysis
- apps/frontend – React + Vite SPA, handles UI, user auth pages (Login/Signup), studio view, SFU client signaling via WebSocket, and uses shared UI components & types.
- apps/user-be – Node service exposing REST endpoints for user & stream metadata, implements controllers, routes, middleware (JWT auth), persists via packages/db (Prisma).
- apps/sfu-server – Node WebSocket SFU (Peer, Room, SFU classes), mediacodec config, signaling utilities, worker threads for scalable media forwarding.
- apps/mixer – Node background service leveraging FFmpeg and YouTube Data API to pull media streams and re-encode/mix for live broadcast.
- packages/common/types – Shared TypeScript message, payload, room, SFU-protocol definitions.
- packages/db – Prisma client and schema; the single source of truth for persistence.
- packages/ui – Shared React components (Button, Card, Code snippet) consumed by frontend.
- packages/{eslint-config,typescript-config,zod} – Build and lint configurations; not part of runtime.

- Browser (frontend SPA):
– UI layer, UI library (packages/ui), shared types.
– SFU client hooks → WebSocket signaling channel to SFU server.
- User-BE (REST API):
– Express/Koa-style HTTP server, JWT auth middleware, controllers for /user and /stream.
– Persists user and stream metadata via Prisma → Postgres/MySQL.
- SFU-Server (WebSocket SFU):
– Signaling channel, Room & Peer managers, media forwarding (no direct DB access).
- Mixer Service (batch/job worker):
– Fetches live streams or recorded segments from SFU or storage → runs FFmpeg pipelines → pushes output to YouTube Live via YouTube API.
- Database:
– Postgres (Prisma).
- External Service:
– YouTube Live Data API.

##### Relationships and data flows
- User-BE ←→ Database: HTTP requests for auth, user data, stream metadata.
- Frontend (HTTP) → User-BE for signup/login and metadata.
- Frontend (WebSocket) ↔ SFU-Server for WebRTC signaling (offer/answer, ICE).
- Clients send/receive RTP media via SFU-Server (UDP or TCP transports managed in utils/transport.ts).
- Mixer polls SFU-Server or pulls from a storage endpoint, runs FFmpeg (apps/mixer/ffmpeg.ts), then pushes to YouTube API (apps/mixer/yt-api.ts).
- All code built, linted, typed via common build configs (turborepo pipelines).

##### Architectural patterns and principles
- Microservices: each app in apps/ is a separate deployable unit.
- Domain-driven layering: controllers → services/utilities → persistence.
- Shared code via packages/ for DRY, strict typing and UI consistency.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        FE_SPA["Frontend SPA<br/>(React, Vite)"]:::frontend
        FE_PAGES["UI Pages<br/>(Login, Signup, Studio)"]:::frontend
        FE_COMPONENTS["UI Components<br/>(Button, Card, Code)"]:::frontend
        FE_HOOKS["SFU Client Hooks<br/>(useSFUClient.tsx, useSFUSocket.tsx)"]:::frontend
        FE_CONFIG["SFU Config<br/>(SFU.ts)"]:::frontend
        COMMON_TYPES["Shared Types<br/>(packages/common/types)"]:::frontend
        UI_LIB["Shared UI Library<br/>(packages/ui)"]:::frontend

        FE_SPA --> FE_PAGES
        FE_SPA --> FE_COMPONENTS
        FE_SPA --> FE_HOOKS
        FE_SPA --> FE_CONFIG
        FE_SPA --> COMMON_TYPES
        FE_SPA --> UI_LIB
    end

    %% API Layer
    subgraph "User-BE Service"
        direction TB
        BE_ENTRY["Entry & Config<br/>(index.ts, conf.ts)"]:::rest
        BE_CONTROLLERS["Controllers<br/>(user.ts, stream.ts)"]:::rest
        BE_ROUTES["Routes<br/>(user.ts, stream.ts, index.ts)"]:::rest
        BE_MW["JWT Auth Middleware<br/>(auth.ts)"]:::rest
        BE_DB_CLIENT["Prisma Client"]:::rest

        BE_ENTRY --> BE_MW
        BE_ENTRY --> BE_ROUTES
        BE_ROUTES --> BE_CONTROLLERS
        BE_CONTROLLERS --> BE_DB_CLIENT
    end

    %% SFU Layer
    subgraph "SFU-Server"
        direction TB
        SFU_ENTRY["Entry<br/>(index.ts)"]:::sfu
        SFU_CORE["Core Classes<br/>(Peer.ts, Room.ts, SFU.ts)"]:::sfu
        SFU_CONF["Config<br/>(conf.ts, mediaCodecs.ts)"]:::sfu
        SFU_UTILS["Utils<br/>(socketMsg.ts, transport.ts, auth.ts, worker.ts)"]:::sfu

        SFU_ENTRY --> SFU_CONF
        SFU_ENTRY --> SFU_CORE
        SFU_ENTRY --> SFU_UTILS
        SFU_CORE --> SFU_UTILS
    end

    %% Mixer Layer
    subgraph "Mixer Service"
        direction TB
        MIXER_ENTRY["Entry & Config<br/>(index.ts, conf.ts, auth.ts)"]:::mixer
        MIXER_FFMPEG["FFmpeg Logic<br/>(ffmpeg.ts)"]:::mixer
        MIXER_YT["YouTube API<br/>(yt-api.ts)"]:::mixer
        MIXER_TYPES["Mixer Types<br/>(types.ts)"]:::mixer

        MIXER_ENTRY --> MIXER_FFMPEG
        MIXER_ENTRY --> MIXER_YT
        MIXER_ENTRY --> MIXER_TYPES
    end

    %% Datastore and External
    DB["Database<br/>(Postgres via Prisma)"]:::db
    YT["YouTube Live API"]:::external

    %% Shared DB package
    DB_PKG["DB Package<br/>(packages/db)"]:::db

    %% Dependencies
    BE_DB_CLIENT --> DB_PKG
    DB_PKG --> DB

    %% Flows
    FE_SPA -->|"HTTP/JSON"| BE_ENTRY
    BE_ENTRY -->|"ORM Queries"| DB
    FE_HOOKS -->|"WebSocket Signaling"| SFU_ENTRY
    SFU_CORE -->|"RTP Media (UDP/TCP)"| SFU_UTILS
    MIXER_FFMPEG -->|"Pull Streams"| SFU_ENTRY
    MIXER_YT -->|"REST/SDK Calls"| YT

    %% Click events
    click FE_SPA "https://github.com/cb7chaitanya/streamly/tree/main/apps/frontend"
    click FE_PAGES "https://github.com/cb7chaitanya/streamly/tree/main/apps/frontend/src/pages"
    click FE_COMPONENTS "https://github.com/cb7chaitanya/streamly/tree/main/apps/frontend/src/components"
    click FE_HOOKS "https://github.com/cb7chaitanya/streamly/blob/main/apps/frontend/src/hooks/useSFUClient.tsx"
    click FE_HOOKS "https://github.com/cb7chaitanya/streamly/blob/main/apps/frontend/src/hooks/useSFUSocket.tsx"
    click FE_CONFIG "https://github.com/cb7chaitanya/streamly/blob/main/apps/frontend/src/config/SFU.ts"
    click UI_LIB "https://github.com/cb7chaitanya/streamly/tree/main/packages/ui/src"
    click COMMON_TYPES "https://github.com/cb7chaitanya/streamly/tree/main/packages/common/types"
    click BE_ENTRY "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/index.ts"
    click BE_ENTRY "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/conf.ts"
    click BE_CONTROLLERS "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/controllers/user.ts"
    click BE_CONTROLLERS "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/controllers/stream.ts"
    click BE_ROUTES "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/routes/user.ts"
    click BE_ROUTES "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/routes/stream.ts"
    click BE_ROUTES "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/routes/index.ts"
    click BE_MW "https://github.com/cb7chaitanya/streamly/blob/main/apps/user-be/src/middleware/auth.ts"
    click DB_PKG "https://github.com/cb7chaitanya/streamly/blob/main/packages/db/db.ts"
    click DB_PKG "https://github.com/cb7chaitanya/streamly/blob/main/packages/db/prisma/schema.prisma"
    click SFU_ENTRY "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/index.ts"
    click SFU_CORE "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/Peer.ts"
    click SFU_CORE "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/Room.ts"
    click SFU_CORE "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/SFU.ts"
    click SFU_CONF "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/config/conf.ts"
    click SFU_CONF "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/config/mediaCodecs.ts"
    click SFU_UTILS "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/utils/socketMsg.ts"
    click SFU_UTILS "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/utils/transport.ts"
    click SFU_UTILS "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/utils/auth.ts"
    click SFU_UTILS "https://github.com/cb7chaitanya/streamly/blob/main/apps/sfu-server/src/utils/worker.ts"
    click MIXER_ENTRY "https://github.com/cb7chaitanya/streamly/blob/main/apps/mixer/src/index.ts"
    click MIXER_ENTRY "https://github.com/cb7chaitanya/streamly/blob/main/apps/mixer/src/conf.ts"
    click MIXER_ENTRY "https://github.com/cb7chaitanya/streamly/blob/main/apps/mixer/src/auth.ts"
    click MIXER_FFMPEG "https://github.com/cb7chaitanya/streamly/blob/main/apps/mixer/src/ffmpeg.ts"
    click MIXER_YT "https://github.com/cb7chaitanya/streamly/blob/main/apps/mixer/src/yt-api.ts"
    click MIXER_TYPES "https://github.com/cb7chaitanya/streamly/blob/main/apps/mixer/src/types.ts"

    %% Styles
    classDef frontend fill:#a2f5a2,stroke:#333,stroke-width:1px
    classDef rest fill:#a2d2f5,stroke:#333,stroke-width:1px
    classDef sfu fill:#f5cba2,stroke:#333,stroke-width:1px
    classDef mixer fill:#d3b2f5,stroke:#333,stroke-width:1px
    classDef db fill:#cccccc,stroke:#333,stroke-width:1px
    classDef external fill:#f5e6a2,stroke:#333,stroke-width:1px
```
