# apirtc/react-lib architecture

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

- Interactive diagram: https://gitdiagram.com/apirtc/react-lib
- Repository: https://github.com/apirtc/react-lib

## Overview

##### Project type and purpose
- This is a React component/library project (“@apirtc/react-lib”) that provides a set of React hooks and a UI component (VideoStream) to simplify integration with the ApiRTC real-time communication SDK.
- It is intended to be consumed as a peer‐dependency by any React application needing conferencing, media stream management, presence, conversations, messaging, moderation, etc.

##### File structure
- Top-level configuration and metadata:
- .eslintrc.json, jest.config.js, rollup.config.js, tsconfig.json, package.json, sonar-project.properties
- CHANGELOG.md, LICENSE, README.md
- docs/: Static documentation generated for functions (hooks) and types.
- src/: TypeScript source, split into
- src/hooks/: each hook in its own file plus associated tests (.spec.ts)
- src/components/: VideoStream component with entry index.ts
- src/index.ts: public export surface.
- Build and packaging: Rollup config compiles TS into ESM/CJS bundles, tests run via Jest.

##### Key insights from README
- Lists each hook’s purpose and usage (useSession, useUserMediaDevices, useCameraStream, useStreamApplyAudioProcessor, etc.)
- Mentions VideoStream UI component.
- Describes dependency on @apirtc/apirtc SDK and how hooks wrap SDK calls.

##### Main components
a. React-Lib Module
– Hooks layer (each useXxx hook)
– UI component layer (VideoStream)
– Core export index (public API surface)
b. External Dependencies
– @apirtc/apirtc SDK (peer dependency)
– Browser Web APIs (MediaDevices, getDisplayMedia)
c. Consumer Application
– Host React app importing @apirtc/react-lib, providing apiKey credentials, rendering UI
d. ApiRTC Cloud / Signaling Servers
– Remote real-time servers accessed via @apirtc/apirtc SDK

##### Relationships and data flows
- The Host App calls a hook (e.g., useSession) → hook initializes ApiRTC SDK session → maintains session state.
- Hooks like useUserMediaDevices access Web MediaDevices APIs → return device lists to UI.
- Hooks like useCameraStream call session.getUserMedia() → produce a MediaStream object.
- Hook useStreamApplyAudioProcessor or useStreamApplyVideoProcessor wraps the raw MediaStream via SDK processing.
- useConversation* hooks orchestrate ApiRTC conversation creation, contact list, messaging, moderation and manage remote streams (publish/subscribe).
- VideoStream component takes a MediaStream prop (from any hook) and renders it via a <video> element.
- All SDK network traffic flows to/from ApiRTC Cloud/Servers.

##### Architectural patterns and design principles
- Modular, single-purpose hooks each encapsulate one piece of real-time functionality.
- Separation of concerns: hooks manage state & side-effects; components manage presentation.
- Peer-dependency injection: external SDK (@apirtc/apirtc) is not bundled, ensuring version control.
- Build pipeline: Rollup for tree-shaking, Jest for unit tests with 100% coverage.

##### Final advice
- Be exhaustive: include every hook group, the VideoStream component, and all interactions with the SDK and browser platform.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Host Application
    HostApp["Host React App"]:::host

    %% External Dependencies
    SDK["@apirtc/apirtc SDK"]:::external
    BrowserAPI["Browser MediaDevices / getDisplayMedia"]:::external
    Cloud["ApiRTC Cloud / Signaling Servers"]:::external

    %% React Library Module
    subgraph "React-Lib Module" 
        direction TB

        %% Public API Surface
        PublicAPI["src/index.ts\n(Public API Surface)"]:::lib
        
        %% Hooks Layer
        subgraph "Hooks Layer" 
            direction TB
            HooksLayer["src/hooks/\n(All Hooks)"]:::lib
            useSession["useSession"]:::lib
            useUserMediaDevices["useUserMediaDevices"]:::lib
            useCameraStream["useCameraStream"]:::lib
            useStreamApplyAudioProcessor["useStreamApplyAudioProcessor"]:::lib
            useStreamApplyVideoProcessor["useStreamApplyVideoProcessor"]:::lib
            usePresence["usePresence"]:::lib
            subgraph "Conversation Hooks"
                direction TB
                useConversation["useConversation"]:::lib
                useConversationContacts["useConversationContacts"]:::lib
                useConversationMessages["useConversationMessages"]:::lib
                useConversationModeration["useConversationModeration"]:::lib
                useConversationStreams["useConversationStreams"]:::lib
            end
        end

        %% UI Components Layer
        subgraph "UI Component Layer"
            direction TB
            VideoStreamCompFolder["src/components/VideoStream/\n(VideoStream Component)"]:::lib
            VideoStreamImpl["VideoStream.tsx\n(Implementation)"]:::lib
            VideoStreamIndex["index.ts\n(Public Entry)"]:::lib
        end
    end

    %% Build and Docs Pipeline
    subgraph "Build & CI/CD" 
        direction TB
        Rollup["rollup.config.js\n(Rollup Build)"]:::tool
        Jest["jest.config.js\n(Jest Tests)"]:::tool
        TSConfig["tsconfig.json\n(TypeScript Config)"]:::tool
        Docs["docs/\n(Typedoc Output)"]:::tool
    end

    %% Connections
    HostApp -->|"import from"| PublicAPI
    PublicAPI -->|exposes| HooksLayer
    PublicAPI -->|exposes| VideoStreamCompFolder

    %% Hooks to SDK and Browser
    useSession -->|"initSession()"| SDK
    useUserMediaDevices -->|"getUserMedia()/enumerate()"| BrowserAPI
    useCameraStream -->|"session.getUserMedia()"| SDK
    useCameraStream -->|"getUserMedia()"| BrowserAPI
    useStreamApplyAudioProcessor -->|"applyAudioProcessor()"| SDK
    useStreamApplyVideoProcessor -->|"applyVideoProcessor()"| SDK

    %% Conversation hooks
    useConversation -->|"createConversation()"| SDK
    useConversationContacts -->|"listContacts()"| SDK
    useConversationMessages -->|"send/receive"| SDK
    useConversationModeration -->|"moderate()"| SDK
    useConversationStreams -->|"publish/subscribe"| SDK

    %% SDK to Cloud
    SDK -->|"signal/data"| Cloud

    %% VideoStream renders MediaStream
    VideoStreamImpl -->|"renders <video>"| BrowserAPI
    useCameraStream -->|provides MediaStream| VideoStreamImpl
    useStreamApplyVideoProcessor -->|provides MediaStream| VideoStreamImpl
    useConversationStreams -->|provides MediaStream| VideoStreamImpl

    %% Build/Test pipelines
    HooksLayer --> Rollup
    VideoStreamCompFolder --> Rollup
    PublicAPI --> Rollup
    srcIndexClick[/"source"/] -.-> Jest
    Jest -->|"runs tests in"| HooksLayer
    Jest -->|"runs tests in"| VideoStreamCompFolder
    TSConfig --> Rollup
    TSConfig --> Jest
    Rollup --> Docs

    %% Click Events
    click HooksLayer "https://github.com/apirtc/react-lib/tree/main/src/hooks/"
    click useSession "https://github.com/apirtc/react-lib/blob/main/src/hooks/useSession.ts"
    click useUserMediaDevices "https://github.com/apirtc/react-lib/blob/main/src/hooks/useUserMediaDevices.ts"
    click useCameraStream "https://github.com/apirtc/react-lib/blob/main/src/hooks/useCameraStream.ts"
    click useStreamApplyAudioProcessor "https://github.com/apirtc/react-lib/blob/main/src/hooks/useStreamApplyAudioProcessor.ts"
    click useStreamApplyVideoProcessor "https://github.com/apirtc/react-lib/blob/main/src/hooks/useStreamApplyVideoProcessor.ts"
    click useConversation "https://github.com/apirtc/react-lib/blob/main/src/hooks/useConversation.ts"
    click useConversationContacts "https://github.com/apirtc/react-lib/blob/main/src/hooks/useConversationContacts.ts"
    click useConversationMessages "https://github.com/apirtc/react-lib/blob/main/src/hooks/useConversationMessages.ts"
    click useConversationModeration "https://github.com/apirtc/react-lib/blob/main/src/hooks/useConversationModeration.ts"
    click useConversationStreams "https://github.com/apirtc/react-lib/blob/main/src/hooks/useConversationStreams.ts"
    click usePresence "https://github.com/apirtc/react-lib/blob/main/src/hooks/usePresence.ts"
    click VideoStreamCompFolder "https://github.com/apirtc/react-lib/tree/main/src/components/VideoStream/"
    click VideoStreamImpl "https://github.com/apirtc/react-lib/blob/main/src/components/VideoStream/VideoStream.tsx"
    click VideoStreamIndex "https://github.com/apirtc/react-lib/blob/main/src/components/VideoStream/index.ts"
    click PublicAPI "https://github.com/apirtc/react-lib/blob/main/src/index.ts"
    click Docs "https://github.com/apirtc/react-lib/tree/main/docs/"
    click Rollup "https://github.com/apirtc/react-lib/blob/main/rollup.config.js"
    click Jest "https://github.com/apirtc/react-lib/blob/main/jest.config.js"
    click TSConfig "https://github.com/apirtc/react-lib/blob/main/tsconfig.json"

    %% Styles
    classDef lib fill:#D0E8FF,stroke:#0366d6,stroke-width:1px
    classDef external fill:#DFFFE0,stroke:#228B22,stroke-width:1px
    classDef host fill:#FFF4D0,stroke:#DAA520,stroke-width:1px
    classDef tool fill:#F0F0F0,stroke:#888888,stroke-width:1px
```
