How madan686/realtime-chatapp works
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: SecurityConfig.java HTTP security chain · ChatMessageService.java Message service · WebSocketConfig.java STOMP/SockJS gateway · app.js Vanilla JS client
Components 18
Browser client
- Vanilla JS client · Login, room lifecycle, history, and STOMP client.
src/main/resources/static/app.js - Static pages
src/main/resources/static/index.html
HTTP application
- HTTP security chain · Protects HTTP APIs; WebSocket handshake JWT auth is not implemented.
src/main/java/com/example/chatapp/config/SecurityConfig.java - JWT authentication filter
src/main/java/com/example/chatapp/config/JwtAuthenticationFilter.java - Auth API and service
src/main/java/com/example/chatapp/controller/AuthController.java - JWT and user lookup
src/main/java/com/example/chatapp/security/JwtService.java - Room API · Creates rooms and validates password-protected joins.
src/main/java/com/example/chatapp/controller/RoomController.java - History API
src/main/java/com/example/chatapp/controller/MessageRestController.java
Real-time messaging
- STOMP/SockJS gateway · Exposes /ws and application/topic routing.
src/main/java/com/example/chatapp/config/WebSocketConfig.java - Chat command handler · Handles send, private send, join, and leave commands.
src/main/java/com/example/chatapp/controller/ChatController.java - Message service · Persists events and coordinates distribution.
src/main/java/com/example/chatapp/service/ChatMessageService.java - Redis publisher
src/main/java/com/example/chatapp/service/RedisPublisherService.java - Redis subscriber · Broadcasts events received from peer instances.
src/main/java/com/example/chatapp/service/RedisSubscriberService.java
Data and distribution
- User records
src/main/java/com/example/chatapp/repository/AppUserRepository.java - Room records
src/main/java/com/example/chatapp/repository/ChatRoomRepository.java - Chat history · Durable system of record for messages.
src/main/java/com/example/chatapp/repository/ChatMessageRepository.java - Redis pub/sub · Cross-instance chat-event distribution.
src/main/java/com/example/chatapp/config/RedisConfig.java
Other components
- Spring Boot application
src/main/java/com/example/chatapp/ChatAppApplication.java
Connections 21
- 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