How misterneo/spring-microservices-chat-app works
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.