# laminhtam/social-media-web-app architecture

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

- Interactive diagram: https://gitdiagram.com/laminhtam/social-media-web-app
- Repository: https://github.com/laminhtam/social-media-web-app

## Overview

##### Main Components
- Frontend: The “social-media-fe” directory is a Next.js project that interacts with the backend via HTTP or WebSocket.
- API Gateway: Acts as a single-entry point for frontend requests and routes them to the appropriate microservices while handling authentication via JWT and other security protocols.
- Auth Service: Manages user authentication and authorization (login, sign-up, token refresh, logout).
- Chat Service: Handles real-time messaging through WebSocket, including managing conversation and message flows.
- Notification Service: Relays notifications to users and supports features such as video calls (one-to-one and group calls) via WebSocket, and integrates with RabbitMQ for message-driven communication.
- Post Service: Manages feeds, posts, comments, reactions, and integrates with both MongoDB (for document-based content) and Neo4j (for relationship-based queries like categories or friend graphs).
- User Service: Manages user-related operations such as friend requests and user profiles.
- Config Server: Centralizes all microservice configuration properties, reading from YAML files.
- Service Registry: Enables service discovery so that microservices can dynamically register and communicate.

##### Relationships and Data Flow
- The Frontend communicates with the API Gateway.
- The API Gateway authenticates incoming requests and then routes them to the required microservice (Auth, Chat, Post, User, etc.).
- Each microservice registers with the Service Registry and retrieves its configurations from the Config Server.
- For asynchronous communications, services like the Chat and Notification services use RabbitMQ for reliable message brokering.
- Redis is used as a caching layer across several services (e.g., the API Gateway, Auth Service, Notification Service) to improve performance.
- Real-time messaging is enabled via WebSocket on both the frontend and one or more dedicated services (Notification/Chat).
- Databases: MySQL (if used for relational components), MongoDB (in Post Service for storing posts/comments), and Neo4j (for managing graph-based relationships in the Post Service).

##### Architectural Patterns and Technologies
- Microservices: Each service is self-contained and communicates via RESTful endpoints and messaging queues.
- API Gateway Pattern: Centralizes request routing, authentication, and sometimes caching.
- Service Discovery: Enabled by the Service Registry for dynamic lookup.
- Centralized Configuration: Managed by the Config Server.
- Asynchronous Messaging: Provided via RabbitMQ.
- Real-time Communication: Implemented with WebSocket and Spring WebFlux for reactive, non-blocking operations.
- Caching: Redis is used to cut down on load and improve latency.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Layer
    subgraph "Client Layer"
        FE("Frontend (Next.js)"):::frontend
    end

    %% API Gateway Layer
    subgraph "API & Gateway Layer"
        AG("API Gateway"):::gateway
    end

    %% Backend Microservices
    subgraph "Microservices"
        Auth("Auth Service"):::microservice
        Chat("Chat Service"):::microservice
        Notif("Notification Service"):::microservice
        Post("Post Service"):::microservice
        User("User Service"):::microservice
    end

    %% Supporting Infrastructure
    subgraph "Infrastructure"
        SR("Service Registry"):::infrastructure
        CS("Config Server"):::infrastructure
        RMQ("RabbitMQ"):::messaging
        Redis("Redis Cache"):::infrastructure
        MySQL("MySQL"):::database
        Mongo("MongoDB"):::database
        Neo("Neo4j"):::database
    end

    %% Data Flow from Frontend to API Gateway
    FE -->|"HTTP/WebSocket"| AG

    %% API Gateway Routing to Microservices
    AG -->|"routes"| Auth
    AG -->|"routes"| Chat
    AG -->|"routes"| Notif
    AG -->|"routes"| Post
    AG -->|"routes"| User

    %% Microservices registering with Service Registry
    Auth -->|"registers"| SR
    Chat -->|"registers"| SR
    Notif -->|"registers"| SR
    Post -->|"registers"| SR
    User -->|"registers"| SR
    AG -->|"config"| CS

    %% Microservices retrieving configuration from Config Server
    Auth -->|"config"| CS
    Chat -->|"config"| CS
    Notif -->|"config"| CS
    Post -->|"config"| CS
    User -->|"config"| CS

    %% Asynchronous Messaging via RabbitMQ
    Chat -->|"pushes"| RMQ
    Notif -->|"receives"| RMQ

    %% Caching via Redis
    AG -->|"caches"| Redis
    Auth -->|"caches"| Redis
    Notif -->|"caches"| Redis

    %% Database Connections
    Auth -->|"stores-relational"| MySQL
    Post -->|"stores documents"| Mongo
    Post -->|"stores-relations"| Neo

    %% Click Events
    click FE "https://github.com/laminhtam/social-media-web-app/tree/main/social-media-fe"
    click AG "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/api-gateway"
    click Auth "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/auth-service"
    click Chat "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/chat-service"
    click Notif "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/notification-service"
    click Post "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/post-service"
    click User "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/user-service"
    click CS "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/config-server"
    click SR "https://github.com/laminhtam/social-media-web-app/tree/main/SocialMediaBE/service-registry"

    %% Styles
    classDef frontend fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef gateway fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef microservice fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#98FB98,stroke:#333,stroke-width:2px;
    classDef messaging fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef database fill:#D3D3D3,stroke:#333,stroke-width:2px;
```
