# ngo-son-truong/spring-boot-webrtc architecture

> Architecture diagram of the GitHub repository ngo-son-truong/spring-boot-webrtc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ngo-son-truong/spring-boot-webrtc
- Repository: https://github.com/ngo-son-truong/spring-boot-webrtc

## Overview

##### Project type and purpose
- This is a full-stack, single-module Spring Boot web application that provides real-time, peer-to-peer video/audio calls using WebRTC.
- The frontend is server-rendered with Thymeleaf and Bootstrap, enhanced by JavaScript for WebRTC and WebSocket signaling.
- The backend exposes:
- HTTP endpoints for page rendering and authentication (AuthController, VideoCallController)
- A STOMP-over-SockJS WebSocket endpoint for signaling (WebRTCSignalController)

##### File structure
- Top-level: pom.xml, Maven wrapper, README
- src/main/java/com/project/webrtc
- config: WebMvcConfig, WebSocketConfig, AuthInterceptor
- controller: AuthController, VideoCallController, WebRTCSignalController
- service: RoomService, UserService
- model: User, Room, WebRTCMessage
- WebRtcApplication.java (Spring Boot entry point)
- src/main/resources
- application.yml (configuration)
- static/css (styles.css)
- templates (index.html, login, register, room)
- No external database folder—services likely use in-memory or simple persistence.

##### Key architecture points from README
- Signaling via WebSocket (STOMP over SockJS)
- P2P media uses WebRTC APIs (with STUN; TURN optional)
- Multi-user rooms, screen sharing, camera switching

##### Main components
a. Clients (Browsers)
- UI: Thymeleaf-rendered pages + Bootstrap + custom JS
- WebSocket client (SockJS + STOMP) for signaling
- WebRTC PeerConnections for media streams
b. Spring Boot Server
- HTTP layer (AuthController, VideoCallController)
- WebSocket Signaling layer (WebRTCSignalController)
- Interceptor for auth (AuthInterceptor)
- Services (RoomService, UserService)
- Models (Room, User, WebRTCMessage)
c. External Services
- STUN (and TURN) servers for ICE negotiation
d. (Optional) Data Store
- In-memory map or persistent DB (not explicitly shown but can be added for production)

##### Relationships and interactions
- User opens browser → HTTP GET / → Thymeleaf page
- User registers/logs in → AuthController → (UserService persists user)
- User creates/joins room → VideoCallController → Thymeleaf room.html
- room.html JS → opens SockJS connection → STOMP subscribe to /topic/room/{id}
- room.html JS → sends signaling messages (offer/answer/ICE) via WebSocket to WebRTCSignalController
- WebRTCSignalController → broadcasts messages to other participants’ STOMP subscriptions
- Clients establish direct peer connections using ICE (STUN/TURN)
- Media flows peer-to-peer; signaling flows via server

##### Architectural patterns & design principles
- MVC for HTTP layer (Controllers → Services → Models)
- Single Exchange message bus for signaling (STOMP topics)
- Separation of concerns:
- Config classes isolate Web MVC and WebSocket setup
- Controllers isolate HTTP vs. signaling logic
- Services encapsulate business logic (room and user management)

##### Diagramming guidance
- HTTP requests/ responses between Client UI and Auth/VideoCall controllers
- WebSocket messages between Client SockJS and WebRTCSignalController
- ICE negotiation through STUN/TURN servers
- Blue for Client-side (UI, signaling, media)
- Green for Server components (controllers, services, configs)
- Orange for External services (STUN/TURN)
- Rectangles for modules
- Clouds for external services

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Side
    subgraph "Browser Client" 
        direction TB
        UI["Thymeleaf UI\n(CSS/JS)"]:::frontend
        SignalingClient["SockJS/STOMP Client"]:::frontend
        PeerConn["WebRTC PeerConnection"]:::frontend
    end

    %% Server Side
    subgraph "Spring Boot Server"
        direction TB
        Entry["WebRtcApplication"]:::backend
        subgraph "Config Layer"
            WebMvcCfg["WebMvcConfig"]:::config
            WebSocketCfg["WebSocketConfig"]:::config
            AuthInt["AuthInterceptor"]:::config
        end
        subgraph "HTTP Layer"
            AuthCtrl["AuthController"]:::controller
            VideoCtrl["VideoCallController"]:::controller
        end
        subgraph "WebSocket Layer"
            SignalCtrl["WebRTCSignalController"]:::controller
        end
        subgraph "Service Layer"
            RoomSvc["RoomService"]:::service
            UserSvc["UserService"]:::service
        end
        subgraph "Model Layer"
            UserModel["User"]:::model
            RoomModel["Room"]:::model
            MsgModel["WebRTCMessage"]:::model
        end
    end

    %% External Services
    STUN["STUN/TURN Servers"]:::external

    %% Client-Server Interactions
    UI -->|"HTTP GET/POST"| AuthCtrl
    UI -->|"HTTP GET/POST"| VideoCtrl
    SignalingClient -->|"STOMP over WebSocket"| SignalCtrl
    SignalCtrl -->|"Broadcast STOMP\n(topic) "| SignalingClient

    %% Peer-to-peer Media
    PeerConn <-->|"Media Streams\n(RTP/RTCP)"| PeerConn
    PeerConn -->|"ICE\n(Stun/TURN)"| STUN

    %% Server Internal Flows
    AuthCtrl -->|"uses"| UserSvc
    VideoCtrl -->|"uses"| RoomSvc
    SignalCtrl -->|"uses"| RoomSvc
    SignalCtrl -->|"uses"| UserSvc
    AuthInt --> AuthCtrl
    AuthInt --> SignalCtrl
    WebMvcCfg --> AuthCtrl
    WebSocketCfg --> SignalCtrl
    Entry --> WebMvcCfg
    Entry --> WebSocketCfg
    Entry --> AuthInt
    RoomSvc --> RoomModel
    UserSvc --> UserModel
    SignalCtrl --> MsgModel

    %% Click Events
    click Entry "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/WebRtcApplication.java"
    click AuthCtrl "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/controller/AuthController.java"
    click VideoCtrl "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/controller/VideoCallController.java"
    click SignalCtrl "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/controller/WebRTCSignalController.java"
    click WebMvcCfg "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/config/WebMvcConfig.java"
    click WebSocketCfg "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/config/WebSocketConfig.java"
    click AuthInt "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/config/AuthInterceptor.java"
    click RoomSvc "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/service/RoomService.java"
    click UserSvc "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/service/UserService.java"
    click UserModel "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/model/User.java"
    click RoomModel "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/model/Room.java"
    click MsgModel "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/java/com/project/webrtc/model/WebRTCMessage.java"
    click ApplicationYML "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/resources/application.yml"
    click Styles "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/resources/static/css/styles.css"
    click Index "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/resources/templates/index.html"
    click Login "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/resources/templates/login.html"
    click Register "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/resources/templates/register.html"
    click RoomHTML "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/main/resources/templates/room.html"
    click Pom "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/pom.xml"
    click Tests "https://github.com/ngo-son-truong/spring-boot-webrtc/blob/main/src/test/java/com/project/webrtc/WebRtcApplicationTests.java"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#004EAA,color:#004EAA;
    classDef backend fill:#E2F0D9,stroke:#317A00,color:#317A00;
    classDef config fill:#FFF4C2,stroke:#B38F00,color:#B38F00;
    classDef controller fill:#DAE8FC,stroke:#6C8EBF,color:#6C8EBF;
    classDef service fill:#EEDDEF,stroke:#B854A4,color:#B854A4;
    classDef model fill:#F8E0E6,stroke:#C2314B,color:#C2314B;
    classDef external fill:#FFE2CC,stroke:#CC8000,color:#CC8000;
```
