# madan686/realtime-chatapp architecture

> Architecture diagram of the GitHub repository madan686/realtime-chatapp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/madan686/realtime-chatapp
- Repository: https://github.com/madan686/realtime-chatapp

## Overview

#### 1. Purpose and Runtime
A Spring Boot 3 / Java 21 real-time chat service serving its own vanilla-JS browser client. It combines JWT-backed HTTP authentication and room management with STOMP-over-SockJS WebSocket messaging. MongoDB persists users, rooms, and chat history; Redis is additionally wired for cross-instance chat-event publishing/subscription. Main bootstrap: `src/main/java/com/example/chatapp/ChatAppApplication.java`. Packaging/deployment uses `pom.xml` and `Dockerfile`.

#### 2. Client and Entry Boundaries
Static pages and client-side STOMP/JWT behavior are served directly by Spring:
- `src/main/resources/static/login.html`
- `src/main/resources/static/register.html`
- `src/main/resources/static/index.html`
- `src/main/resources/static/app.js`

The browser authenticates, retains its token, manages room lifecycle through HTTP, then connects to `/ws` and publishes/subscribes to the documented STOMP destinations. The frontend is a thin client; authorization, persistence, room validation, and message routing remain server-side.

#### 3. Authentication and Security Pipeline
Authentication endpoints are handled through `src/main/java/com/example/chatapp/controller/AuthController.java` and orchestration in `src/main/java/com/example/chatapp/service/AuthService.java`. User records are modeled and stored through:
- `src/main/java/com/example/chatapp/model/AppUser.java`
- `src/main/java/com/example/chatapp/repository/AppUserRepository.java`

JWT creation/validation and identity lookup reside in `src/main/java/com/example/chatapp/security/JwtService.java` and `src/main/java/com/example/chatapp/security/CustomUserDetailsService.java`. `src/main/java/com/example/chatapp/config/JwtAuthenticationFilter.java` applies token authentication to HTTP requests under rules defined in `src/main/java/com/example/chatapp/config/SecurityConfig.java`. README explicitly identifies WebSocket handshake JWT authentication as future work, so WebSocket security is a material boundary from the HTTP filter chain.

#### 4. Room Lifecycle and Persistence
HTTP room APIs are exposed by `src/main/java/com/example/chatapp/controller/RoomController.java`, using DTOs such as `src/main/java/com/example/chatapp/dto/CreateRoomRequest.java` and `src/main/java/com/example/chatapp/dto/JoinRoomRequest.java`. Room state is represented by `src/main/java/com/example/chatapp/model/ChatRoom.java` and persisted through `src/main/java/com/example/chatapp/repository/ChatRoomRepository.java`.

The expected flow is create room → validate room/password on join → enter its topic. Room membership establishes the routing context for public WebSocket broadcasts and history retrieval.

#### 5. Real-Time Message Processing
`src/main/java/com/example/chatapp/config/WebSocketConfig.java` configures the `/ws` STOMP/SockJS transport and application/topic routing. `src/main/java/com/example/chatapp/controller/ChatController.java` receives WebSocket commands for send, private send, join, and leave.

Messages use:
- `src/main/java/com/example/chatapp/model/ChatMessage.java`
- `src/main/java/com/example/chatapp/model/MessageType.java`
- `src/main/java/com/example/chatapp/service/ChatMessageService.java`

Public messages are routed to room topics (`/topic/room/{roomId}`); private messages target user topics (`/topic/private/{username}`). Join/leave are represented as system-style message events, allowing presence notifications through the same delivery path.

#### 6. History and Storage Boundary
`src/main/java/com/example/chatapp/controller/MessageRestController.java` exposes REST access to persisted history, likely consumed when a user enters a room. `ChatMessageService` mediates durable message operations, backed by `src/main/java/com/example/chatapp/repository/ChatMessageRepository.java`. MongoDB connectivity and runtime settings belong in `src/main/resources/application.properties`; MongoDB may be local or Atlas in deployment.

#### 7. Redis Event Distribution
Redis configuration and event fan-out are separated from direct WebSocket handling:
- `src/main/java/com/example/chatapp/config/RedisConfig.java`
- `src/main/java/com/example/chatapp/service/RedisPublisherService.java`
- `src/main/java/com/example/chatapp/service/RedisSubscriberService.java`
- `src/main/java/com/example/chatapp/model/RedisChatEvent.java`

This layer enables a message accepted by one application instance to be published through Redis and consumed/broadcast by peers, addressing horizontal scaling beyond an in-process STOMP broker. MongoDB remains the durable system of record; Redis is the transient distribution channel.

Main entry points: [`src/main/java/com/example/chatapp/config/SecurityConfig.java`](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/SecurityConfig.java) (HTTP security chain), [`src/main/java/com/example/chatapp/service/ChatMessageService.java`](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/service/ChatMessageService.java) (Message service), [`src/main/java/com/example/chatapp/config/WebSocketConfig.java`](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/WebSocketConfig.java) (STOMP/SockJS gateway), [`src/main/resources/static/app.js`](https://github.com/madan686/realtime-chatapp/blob/main/src/main/resources/static/app.js) (Vanilla JS client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser client"]
  node_browser["Vanilla JS client<br/>browser UI<br/>[app.js]"]
  node_pages["Static pages<br/>HTML<br/>[index.html]"]
end

subgraph group_http["HTTP application"]
  node_security["HTTP security chain<br/>Spring Security"]
  node_jwt_filter["JWT authentication filter<br/>HTTP filter"]
  node_auth["Auth API and service<br/>authentication"]
  node_jwt["JWT and user lookup<br/>security service<br/>[JwtService.java]"]
  node_rooms["Room API<br/>REST controller"]
  node_history["History API<br/>REST controller"]
end

subgraph group_realtime["Real-time messaging"]
  node_ws_config["STOMP/SockJS gateway<br/>WebSocket config"]
  node_chat_controller["Chat command handler<br/>STOMP controller"]
  node_message_service["Message service<br/>message orchestration"]
  node_redis_publisher["Redis publisher<br/>event publisher"]
  node_redis_subscriber["Redis subscriber<br/>event subscriber"]
end

subgraph group_data["Data and distribution"]
  node_users[("User records<br/>Mongo repository")]
  node_room_store[("Room records<br/>Mongo repository")]
  node_message_store[("Chat history<br/>Mongo repository")]
  node_redis["Redis pub/sub<br/>transient event bus<br/>[RedisConfig.java]"]
end

node_app{{"Spring Boot application<br/>runtime bootstrap"}}

node_pages -->|"loads"| node_browser
node_browser -->|"HTTP requests with JWT"| node_security
node_security -->|"applies"| node_jwt_filter
node_jwt_filter -->|"validates token"| node_jwt
node_security -->|"auth endpoints"| node_auth
node_auth -->|"issues JWT"| node_jwt
node_auth -->|"stores users"| node_users
node_security -->|"authorized room APIs"| node_rooms
node_rooms -->|"create and validate room"| node_room_store
node_security -->|"authorized history API"| node_history
node_history -->|"loads history"| node_message_service
node_browser -.->|"STOMP over SockJS /ws"| node_ws_config
node_ws_config -->|"routes commands"| node_chat_controller
node_chat_controller -->|"message and presence events"| node_message_service
node_message_service -->|"persists history"| node_message_store
node_message_service -->|"publishes accepted events"| node_redis_publisher
node_redis_publisher -->|"publish"| node_redis
node_redis -->|"subscribe"| node_redis_subscriber
node_redis_subscriber -->|"broadcasts room/user events"| node_ws_config
node_app -->|"boots"| node_security
node_app -->|"boots"| node_ws_config

click node_browser "https://github.com/madan686/realtime-chatapp/blob/main/src/main/resources/static/app.js"
click node_pages "https://github.com/madan686/realtime-chatapp/blob/main/src/main/resources/static/index.html"
click node_security "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/SecurityConfig.java"
click node_jwt_filter "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/JwtAuthenticationFilter.java"
click node_auth "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/AuthController.java"
click node_jwt "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/security/JwtService.java"
click node_rooms "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/RoomController.java"
click node_history "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/MessageRestController.java"
click node_ws_config "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/WebSocketConfig.java"
click node_chat_controller "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/ChatController.java"
click node_message_service "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/service/ChatMessageService.java"
click node_redis_publisher "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/service/RedisPublisherService.java"
click node_redis_subscriber "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/service/RedisSubscriberService.java"
click node_users "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/repository/AppUserRepository.java"
click node_room_store "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/repository/ChatRoomRepository.java"
click node_message_store "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/repository/ChatMessageRepository.java"
click node_redis "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/RedisConfig.java"
click node_app "https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/ChatAppApplication.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_pages toneBlue
class node_security,node_jwt_filter,node_auth,node_jwt,node_rooms,node_history toneAmber
class node_ws_config,node_chat_controller,node_message_service,node_redis_publisher,node_redis_subscriber toneMint
class node_users,node_room_store,node_message_store,node_redis toneRose
class node_app toneNeutral
```

## Components

### Browser client

- [Vanilla JS client](https://github.com/madan686/realtime-chatapp/blob/main/src/main/resources/static/app.js): browser UI · Login, room lifecycle, history, and STOMP client. · `src/main/resources/static/app.js`
- [Static pages](https://github.com/madan686/realtime-chatapp/blob/main/src/main/resources/static/index.html): HTML · `src/main/resources/static/index.html`

### HTTP application

- [HTTP security chain](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/SecurityConfig.java): Spring Security · Protects HTTP APIs; WebSocket handshake JWT auth is not implemented. · `src/main/java/com/example/chatapp/config/SecurityConfig.java`
- [JWT authentication filter](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/JwtAuthenticationFilter.java): HTTP filter · `src/main/java/com/example/chatapp/config/JwtAuthenticationFilter.java`
- [Auth API and service](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/AuthController.java): authentication · `src/main/java/com/example/chatapp/controller/AuthController.java`
- [JWT and user lookup](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/security/JwtService.java): security service · `src/main/java/com/example/chatapp/security/JwtService.java`
- [Room API](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/RoomController.java): REST controller · Creates rooms and validates password-protected joins. · `src/main/java/com/example/chatapp/controller/RoomController.java`
- [History API](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/MessageRestController.java): REST controller · `src/main/java/com/example/chatapp/controller/MessageRestController.java`

### Real-time messaging

- [STOMP/SockJS gateway](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/WebSocketConfig.java): WebSocket config · Exposes /ws and application/topic routing. · `src/main/java/com/example/chatapp/config/WebSocketConfig.java`
- [Chat command handler](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/controller/ChatController.java): STOMP controller · Handles send, private send, join, and leave commands. · `src/main/java/com/example/chatapp/controller/ChatController.java`
- [Message service](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/service/ChatMessageService.java): message orchestration · Persists events and coordinates distribution. · `src/main/java/com/example/chatapp/service/ChatMessageService.java`
- [Redis publisher](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/service/RedisPublisherService.java): event publisher · `src/main/java/com/example/chatapp/service/RedisPublisherService.java`
- [Redis subscriber](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/service/RedisSubscriberService.java): event subscriber · Broadcasts events received from peer instances. · `src/main/java/com/example/chatapp/service/RedisSubscriberService.java`

### Data and distribution

- [User records](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/repository/AppUserRepository.java): Mongo repository · `src/main/java/com/example/chatapp/repository/AppUserRepository.java`
- [Room records](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/repository/ChatRoomRepository.java): Mongo repository · `src/main/java/com/example/chatapp/repository/ChatRoomRepository.java`
- [Chat history](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/repository/ChatMessageRepository.java): Mongo repository · Durable system of record for messages. · `src/main/java/com/example/chatapp/repository/ChatMessageRepository.java`
- [Redis pub/sub](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/config/RedisConfig.java): transient event bus · Cross-instance chat-event distribution. · `src/main/java/com/example/chatapp/config/RedisConfig.java`

### Other components

- [Spring Boot application](https://github.com/madan686/realtime-chatapp/blob/main/src/main/java/com/example/chatapp/ChatAppApplication.java): runtime bootstrap · `src/main/java/com/example/chatapp/ChatAppApplication.java`

## Connections

- Static pages → Vanilla JS client: loads
- Vanilla JS client → HTTP security chain: HTTP requests with JWT
- HTTP security chain → JWT authentication filter: applies
- JWT authentication filter → JWT and user lookup: validates token
- HTTP security chain → Auth API and service: auth endpoints
- Auth API and service → JWT and user lookup: issues JWT
- Auth API and service → User records: stores users
- HTTP security chain → Room API: authorized room APIs
- Room API → Room records: create and validate room
- HTTP security chain → History API: authorized history API
- History API → Message service: loads history
- Vanilla JS client → STOMP/SockJS gateway: STOMP over SockJS /ws · Outside the HTTP JWT filter boundary.
- STOMP/SockJS gateway → Chat command handler: routes commands
- Chat command handler → Message service: message and presence events
- Message service → Chat history: persists history
- Message service → Redis publisher: publishes accepted events
- Redis publisher → Redis pub/sub: publish
- Redis pub/sub → Redis subscriber: subscribe
- Redis subscriber → STOMP/SockJS gateway: broadcasts room/user events
- Spring Boot application → HTTP security chain: boots
- Spring Boot application → STOMP/SockJS gateway: boots
