|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Spring Boot WebSocket chat room application supporting both single-instance and cluster deployments. Uses STOMP protocol over WebSocket with Redis pub/sub for distributed messaging across multiple instances. |
| 8 | + |
| 9 | +**Tech Stack:** |
| 10 | +- Java 17 |
| 11 | +- Spring Boot 3.0.12 |
| 12 | +- WebSocket + STOMP |
| 13 | +- Redis (for clustering) |
| 14 | +- SockJS (client-side WebSocket fallback) |
| 15 | +- Maven |
| 16 | + |
| 17 | +**UI Access:** http://localhost:8080 |
| 18 | + |
| 19 | +## Build and Run Commands |
| 20 | + |
| 21 | +### Single Instance Mode |
| 22 | +```bash |
| 23 | +# Compile and package |
| 24 | +mvn package -DskipTests |
| 25 | + |
| 26 | +# Run application |
| 27 | +java -jar target/springChatRoom-0.0.1-SNAPSHOT.jar |
| 28 | +``` |
| 29 | + |
| 30 | +### Cluster Mode |
| 31 | +```bash |
| 32 | +# Start Redis (macOS with Homebrew) |
| 33 | +brew services start redis |
| 34 | + |
| 35 | +# Test Redis connection |
| 36 | +redis-cli ping |
| 37 | + |
| 38 | +# Compile and run (same as single mode) |
| 39 | +mvn package -DskipTests |
| 40 | +java -jar target/springChatRoom-0.0.1-SNAPSHOT.jar |
| 41 | + |
| 42 | +# Run multiple instances by changing server.port in application.properties |
| 43 | +``` |
| 44 | + |
| 45 | +### Testing |
| 46 | +```bash |
| 47 | +# Run all tests |
| 48 | +mvn test |
| 49 | + |
| 50 | +# Run single test class |
| 51 | +mvn test -Dtest=ChatControllerTest |
| 52 | + |
| 53 | +# Skip tests during build |
| 54 | +mvn package -DskipTests |
| 55 | +``` |
| 56 | + |
| 57 | +## Architecture |
| 58 | + |
| 59 | +### WebSocket/STOMP Configuration |
| 60 | + |
| 61 | +**Endpoints:** |
| 62 | +- WebSocket connection: `/ws` (with SockJS fallback) |
| 63 | +- Application destination prefix: `/app` (client sends here) |
| 64 | +- Broker destinations: `/topic` (public), `/private` (user-specific) |
| 65 | + |
| 66 | +**Message Types:** |
| 67 | +- `CHAT` - Public chat messages |
| 68 | +- `JOIN` - User joins chat room |
| 69 | +- `LEAVE` - User leaves chat room |
| 70 | +- `PRIVATE` - Direct messages between users |
| 71 | + |
| 72 | +### Redis-Based Clustering |
| 73 | + |
| 74 | +The application uses Redis pub/sub to synchronize messages across multiple instances: |
| 75 | + |
| 76 | +**Redis Channels (application.properties):** |
| 77 | +- `redis.channel.msgToAll` → `websocket.msgToAll` - Public messages |
| 78 | +- `redis.channel.userStatus` → `websocket.userStatus` - Join/leave events |
| 79 | +- `redis.channel.private` → `websocket.privateMsg` - Private messages |
| 80 | +- `redis.set.onlineUsers` → `websocket.onlineUsers` - Redis Set for tracking online users |
| 81 | + |
| 82 | +**Message Flow:** |
| 83 | +1. Instance receives WebSocket message from client |
| 84 | +2. Controller publishes to Redis channel via `redisTemplate.convertAndSend()` |
| 85 | +3. All instances (including sender) receive from Redis via `RedisListenerHandle` |
| 86 | +4. Each instance broadcasts to its connected WebSocket clients via `ChatService` |
| 87 | + |
| 88 | +This ensures users connected to different instances can communicate in real-time. |
| 89 | + |
| 90 | +### Key Components |
| 91 | + |
| 92 | +**Controllers:** |
| 93 | +- `ChatController` - WebSocket message handlers (`/app/chat.*` endpoints) |
| 94 | +- `UserController` - REST API for online users (`/online-users`) |
| 95 | + |
| 96 | +**Services:** |
| 97 | +- `ChatService` - Business logic for sending messages via STOMP |
| 98 | + |
| 99 | +**Redis Integration:** |
| 100 | +- `RedisListenerBean` - Configures Redis message listeners for pub/sub channels |
| 101 | +- `RedisListenerHandle` - Receives Redis messages and routes to `ChatService` |
| 102 | + |
| 103 | +**Listeners:** |
| 104 | +- `WebSocketEventListener` - Handles WebSocket connect/disconnect lifecycle |
| 105 | + - On disconnect: removes user from Redis Set, publishes LEAVE event |
| 106 | + |
| 107 | +**Configuration:** |
| 108 | +- `WebSocketConfig` - STOMP endpoint and broker configuration |
| 109 | + |
| 110 | +**Models:** |
| 111 | +- `ChatMessage` - Main message model with type, sender, recipient, content, timestamp |
| 112 | +- `OnlineUser` - Tracks connected users |
| 113 | +- `Message` - Simplified message structure |
| 114 | + |
| 115 | +### Important Implementation Details |
| 116 | + |
| 117 | +**Private Messaging:** |
| 118 | +- Messages stored in Redis Sets: `websocket.privateMsg.{recipient}.{sender}` |
| 119 | +- Uses `simpMessagingTemplate.convertAndSendToUser()` for targeted delivery |
| 120 | +- History retrieved by querying both sender→recipient and recipient→sender Sets |
| 121 | + |
| 122 | +**User Session Management:** |
| 123 | +- Online users stored in Redis Set `websocket.onlineUsers` |
| 124 | +- Accessible across all instances for consistent state |
| 125 | +- WebSocket session attributes track username |
| 126 | + |
| 127 | +**Horizontal Scaling:** |
| 128 | +- Stateless application instances - all state in Redis |
| 129 | +- No direct inter-instance communication |
| 130 | +- New instances auto-join cluster by subscribing to Redis channels |
| 131 | + |
| 132 | +## Development Notes |
| 133 | + |
| 134 | +- **Lombok** is used - ensure IDE has Lombok plugin installed |
| 135 | +- **FastJSON** (alibaba) for JSON serialization |
| 136 | +- Don't save compiled JS/d.ts files in @lib path (per global instructions) |
| 137 | +- Redis must be running for cluster mode; single mode works without Redis |
0 commit comments