# nqtrang1801/whats-app-spring architecture

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

- Interactive diagram: https://gitdiagram.com/nqtrang1801/whats-app-spring
- Repository: https://github.com/nqtrang1801/whats-app-spring

## Overview

##### Main Components
- Frontend (Angular 19): This is the client-side single-page application. It includes the application’s pages (such as the main chat page), components (e.g., chat-list), services (e.g., for API calls and Keycloak integration), and configuration files.
- Backend (Spring Boot): This component comprises various packages dealing with chat, messaging, user handling, files, notifications, security (with Keycloak integration), WebSocket configuration, and supporting layers such as controllers and services.
- External Systems & Integrations: This includes:
- Keycloak server: Provides authentication and authorization.
- PostgreSQL Database: Used by the backend via Spring Data JPA for storing users, chats, messages, etc.
- WebSockets: For real-time messaging. This is implemented within the Spring Boot backend.

##### Architectural Patterns and Principles
- Separation of Concerns: Emphasize that the UI, business logic, security, and data layers are cleanly separated.
- Modular Design: Both the frontend and backend directories are well-structured into modules/services, following patterns like MVC or service-based design.
- Real-Time Communication: Clearly show that WebSocket is an integral part of the backend layer which directly interacts with the Angular frontend.
- Security Integration: Mark the Keycloak authentication layer as essential for both frontend login/sign-up and backend protected endpoints. Also indicate the presence of token interceptors on the frontend and KeycloakJwtAuthenticationConverter/configurations on the backend.

##### Technologies, Frameworks, and Libraries
- Frontend: Angular 19, RxJS, Angular Material.
- Backend: Spring Boot, Spring Security, Spring Data JPA, and WebSockets.
- Database: PostgreSQL.
- Authentication: Keycloak.
- Containerization/Deployment: Docker Compose (as evident from the docker-compose.yml) for orchestrating multi-container setups.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (Angular 19)"
        FE["Angular SPA"]:::frontend
        FEKey["Keycloak Service (Frontend)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend (Spring Boot)"
        %% REST Controllers
        subgraph "REST Controllers"
            ChatCtrl["ChatController"]:::controller
            MsgCtrl["MessageController"]:::controller
            UserCtrl["UserController"]:::controller
        end
        %% Business Logic / Services
        subgraph "Business Logic / Services"
            ChatSvc["ChatService"]:::service
            MsgSvc["MessageService"]:::service
            UserSvc["UserService"]:::service
        end
        %% Data Access Layer
        subgraph "Data Access Layer"
            ChatRepo["ChatRepository"]:::repository
            MsgRepo["MessageRepository"]:::repository
            UserRepo["UserRepository"]:::repository
        end
        FileSvc["FileService"]:::service
        WSConfig["WebSocketConfig"]:::ws
        BackendSec["Backend Security"]:::security
    end

    %% External Components
    PK["PostgreSQL Database"]:::database
    KC["Keycloak Server"]:::keycloak
    DC["Docker Compose"]:::container

    %% Connections between Frontend and Backend
    FE -->|"REST API"| ChatCtrl
    FE -->|"WebSocket"| WSConfig

    %% Connections within Backend: Controllers to Services
    ChatCtrl -->|"calls"| ChatSvc
    MsgCtrl -->|"calls"| MsgSvc
    UserCtrl -->|"calls"| UserSvc

    %% Connections from Services to Repositories
    ChatSvc -->|"uses"| ChatRepo
    MsgSvc -->|"uses"| MsgRepo
    UserSvc -->|"uses"| UserRepo

    %% Repositories to Database
    ChatRepo -->|"CRUD"| PK
    MsgRepo -->|"CRUD"| PK
    UserRepo -->|"CRUD"| PK

    %% Authentication flows
    FEKey -->|"authenticates"| KC
    BackendSec -->|"authenticates"| KC

    %% Container orchestration (Docker Compose)
    DC -->|"orchestrates"| FE
    DC -->|"orchestrates"| WSConfig
    DC -->|"orchestrates"| PK
    DC -->|"orchestrates"| KC

    %% Click Events
    click FE "https://github.com/nqtrang1801/whats-app-spring/tree/main/whatsapp-ui/"
    click ChatCtrl "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/chat/ChatController.java"
    click MsgCtrl "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/message/MessageController.java"
    click UserCtrl "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/user/UserController.java"
    click ChatSvc "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/chat/ChatService.java"
    click MsgSvc "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/message/MessageService.java"
    click UserSvc "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/user/UserService.java"
    click ChatRepo "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/chat/ChatRepository.java"
    click MsgRepo "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/message/MessageRepository.java"
    click UserRepo "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/user/UserRepository.java"
    click FileSvc "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/file/FileService.java"
    click WSConfig "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/ws/WebSocketConfig.java"
    click BackendSec "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/security/SecurityConfig.java"
    click BackendSec "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp/src/main/java/com/socketapp/whatsapp/security/KeycloakJwtAuthenticationConverter.java"
    click FEKey "https://github.com/nqtrang1801/whats-app-spring/blob/main/whatsapp-ui/src/app/utils/keycloak/keycloak.service.ts"
    click DC "https://github.com/nqtrang1801/whats-app-spring/blob/main/docker-compose.yml"

    %% Styles
    classDef frontend fill:#a2d9ce,stroke:#333,stroke-width:2px;
    classDef controller fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef service fill:#aed6f1,stroke:#333,stroke-width:2px;
    classDef repository fill:#fad7a0,stroke:#333,stroke-width:2px;
    classDef ws fill:#c39bd3,stroke:#333,stroke-width:2px;
    classDef security fill:#f5b7b1,stroke:#333,stroke-width:2px;
    classDef database fill:#d4efdf,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef keycloak fill:#fcf3cf,stroke:#333,stroke-width:2px;
    classDef container fill:#d6eaf8,stroke:#333,stroke-width:2px;
```
