# misterneo/spring-microservices-chat-app architecture

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

- Interactive diagram: https://gitdiagram.com/misterneo/spring-microservices-chat-app
- Repository: https://github.com/misterneo/spring-microservices-chat-app

## Overview

##### Project Type and Purpose
- This is a full-stack, containerized chat application built with microservices.
- Two Spring Boot back-end services (auth-service and chat-service) provide authentication and chat messaging.
- A React-based client (chat-app-client) delivers the user interface.
- Nginx serves static assets, acts as an API gateway on port 80, and routes requests to the appropriate service.

##### Main Components
a. Client (React SPA)
- Served by Nginx as static files (index.html, JS bundle).
- Communicates over HTTP/REST for authentication and over WebSocket/STOMP for real-time chat.
b. API Gateway (Nginx)
- Listens on port 80.
- Routes /auth/** → auth-service, /chat/** and /ws/** → chat-service.
- Serves static assets for the client.
c. Auth Service (Spring Boot)
- Endpoints: /api/auth/login, /api/auth/register, user management.
- Uses Spring Security + JWT.
- Persists users to a relational database (configured in application.properties).
d. Chat Service (Spring Boot)
- Exposes REST endpoints for messages and a WebSocket/STOMP endpoint for real-time messaging.
- Persists chat messages to MongoDB.
- Caches user/session data in Redis for ticket-based WebSocket handshakes.
e. Data Stores
- Relational DB (e.g., PostgreSQL or H2) for auth-service.
- MongoDB for storing chat messages.
- Redis for caching user tickets and session state.
f. Docker & Docker Compose
- Each service + MongoDB + Redis + Nginx run in separate containers.
- Defined in docker-compose.yml for orchestration.

##### Relationships & Data Flow
1. User → HTTP GET http://localhost → Nginx → Serves React SPA.
2. User → REST POST /auth/login → Nginx → auth-service → validate credentials → issue JWT → response.
3. React app stores JWT, attaches it to Authorization headers.
4. User → WebSocket WSS://localhost/ws-chat?token=JWT → Nginx (proxy) → chat-service TicketHandshakeInterceptor validates JWT via auth-service or cached Redis.
5. chat-service ↔ Redis (get/set ticket, user info).
6. chat-service stores/retrieves messages in MongoDB.
7. chat-service POST /messages → Nginx → chat-service → persists in MongoDB → returns DTO.

##### Architectural Patterns & Principles
- Microservices: auth and chat are independent Spring Boot apps.
- API Gateway: Nginx handles routing, static serving, SSL (if configured).
- Security: JWT-based stateless authentication.
- Event/Real-time: WebSocket + STOMP for chat.
- Caching: Redis for session/ticket caching to reduce load on auth-service.
- Containerization: Docker for each component, orchestrated by Docker Compose.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User
    User["User (Browser)"]:::frontend

    %% Docker Compose Cluster
    subgraph "Docker Compose Cluster"
        direction TB
        Nginx["Nginx API Gateway & Static Server"]:::gateway
        React["React SPA Static Files"]:::frontend
        Auth["Auth Service\n(Spring Boot)"]:::service
        Chat["Chat Service\n(Spring Boot)"]:::service
    end

    %% Datastores
    subgraph "Datastores"
        direction TB
        RelDB["Relational DB\n(PostgreSQL/H2)"]:::db
        Mongo["MongoDB"]:::db
        Redis["Redis Cache"]:::cache
    end

    %% Container Orchestration
    Compose["Docker Compose"]:::gateway

    %% Connections
    User -->|"HTTP GET /\nLoad SPA"| Nginx
    Nginx -->|"Serves Static Files"| React
    User -->|"HTTP POST /auth/login\nREST"| Nginx
    Nginx -->|"HTTP REST /auth/**"| Auth
    User -->|"WSS /ws-chat?token=JWT\nSTOMP"| Nginx
    Nginx -->|"WS/STOMP Proxy"| Chat
    Nginx -->|"HTTP REST /chat/**"| Chat

    Auth -->|"JPA/JDBC"| RelDB
    Chat -->|"MongoDB Read/Write"| Mongo
    Chat -.->|"Redis Cache (tickets)"| Redis

    Compose --> Nginx
    Compose --> Auth
    Compose --> Chat
    Compose --> RelDB
    Compose --> Mongo
    Compose --> Redis

    %% Click Events
    click React "https://github.com/misterneo/spring-microservices-chat-app/tree/main/chat-app-client/"
    click Nginx "https://github.com/misterneo/spring-microservices-chat-app/blob/main/nginx.conf"
    click Auth "https://github.com/misterneo/spring-microservices-chat-app/tree/main/auth-service/"
    click Chat "https://github.com/misterneo/spring-microservices-chat-app/tree/main/chat-service/"
    click Compose "https://github.com/misterneo/spring-microservices-chat-app/blob/main/docker-compose.yml"

    %% Styles
    classDef frontend fill:#e0ffe0,stroke:#2d8f2d,stroke-width:2px;
    classDef gateway fill:#cfe2ff,stroke:#1a5fb4,stroke-width:2px;
    classDef service fill:#ffe0b2,stroke:#b35a00,stroke-width:2px;
    classDef db fill:#e6e6fa,stroke:#5b3d99,stroke-width:2px;
    classDef cache fill:#f0e68c,stroke:#b29600,stroke-width:2px;
```
