-
Notifications
You must be signed in to change notification settings - Fork 4
chatroom-dev-006-code-refactor #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yennanliu
wants to merge
3
commits into
main
Choose a base branch
from
chatroom-dev-006-code-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # CLAUDE.md | ||
|
|
||
| This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
|
|
||
| ## Project Overview | ||
|
|
||
| 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. | ||
|
|
||
| **Tech Stack:** | ||
| - Java 17 | ||
| - Spring Boot 3.0.12 | ||
| - WebSocket + STOMP | ||
| - Redis (for clustering) | ||
| - SockJS (client-side WebSocket fallback) | ||
| - Maven | ||
|
|
||
| **UI Access:** http://localhost:8080 | ||
|
|
||
| ## Build and Run Commands | ||
|
|
||
| ### Single Instance Mode | ||
| ```bash | ||
| # Compile and package | ||
| mvn package -DskipTests | ||
|
|
||
| # Run application | ||
| java -jar target/springChatRoom-0.0.1-SNAPSHOT.jar | ||
| ``` | ||
|
|
||
| ### Cluster Mode | ||
| ```bash | ||
| # Start Redis (macOS with Homebrew) | ||
| brew services start redis | ||
|
|
||
| # Test Redis connection | ||
| redis-cli ping | ||
|
|
||
| # Compile and run (same as single mode) | ||
| mvn package -DskipTests | ||
| java -jar target/springChatRoom-0.0.1-SNAPSHOT.jar | ||
|
|
||
| # Run multiple instances by changing server.port in application.properties | ||
| ``` | ||
|
|
||
| ### Testing | ||
| ```bash | ||
| # Run all tests | ||
| mvn test | ||
|
|
||
| # Run single test class | ||
| mvn test -Dtest=ChatControllerTest | ||
|
|
||
| # Skip tests during build | ||
| mvn package -DskipTests | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| ### WebSocket/STOMP Configuration | ||
|
|
||
| **Endpoints:** | ||
| - WebSocket connection: `/ws` (with SockJS fallback) | ||
| - Application destination prefix: `/app` (client sends here) | ||
| - Broker destinations: `/topic` (public), `/private` (user-specific) | ||
|
|
||
| **Message Types:** | ||
| - `CHAT` - Public chat messages | ||
| - `JOIN` - User joins chat room | ||
| - `LEAVE` - User leaves chat room | ||
| - `PRIVATE` - Direct messages between users | ||
|
|
||
| ### Redis-Based Clustering | ||
|
|
||
| The application uses Redis pub/sub to synchronize messages across multiple instances: | ||
|
|
||
| **Redis Channels (application.properties):** | ||
| - `redis.channel.msgToAll` → `websocket.msgToAll` - Public messages | ||
| - `redis.channel.userStatus` → `websocket.userStatus` - Join/leave events | ||
| - `redis.channel.private` → `websocket.privateMsg` - Private messages | ||
| - `redis.set.onlineUsers` → `websocket.onlineUsers` - Redis Set for tracking online users | ||
|
|
||
| **Message Flow:** | ||
| 1. Instance receives WebSocket message from client | ||
| 2. Controller publishes to Redis channel via `redisTemplate.convertAndSend()` | ||
| 3. All instances (including sender) receive from Redis via `RedisListenerHandle` | ||
| 4. Each instance broadcasts to its connected WebSocket clients via `ChatService` | ||
|
|
||
| This ensures users connected to different instances can communicate in real-time. | ||
|
|
||
| ### Key Components | ||
|
|
||
| **Controllers:** | ||
| - `ChatController` - WebSocket message handlers (`/app/chat.*` endpoints) | ||
| - `UserController` - REST API for online users (`/online-users`) | ||
|
|
||
| **Services:** | ||
| - `ChatService` - Business logic for sending messages via STOMP | ||
|
|
||
| **Redis Integration:** | ||
| - `RedisListenerBean` - Configures Redis message listeners for pub/sub channels | ||
| - `RedisListenerHandle` - Receives Redis messages and routes to `ChatService` | ||
|
|
||
| **Listeners:** | ||
| - `WebSocketEventListener` - Handles WebSocket connect/disconnect lifecycle | ||
| - On disconnect: removes user from Redis Set, publishes LEAVE event | ||
|
|
||
| **Configuration:** | ||
| - `WebSocketConfig` - STOMP endpoint and broker configuration | ||
|
|
||
| **Models:** | ||
| - `ChatMessage` - Main message model with type, sender, recipient, content, timestamp | ||
| - `OnlineUser` - Tracks connected users | ||
| - `Message` - Simplified message structure | ||
|
|
||
| ### Important Implementation Details | ||
|
|
||
| **Private Messaging:** | ||
| - Messages stored in Redis Sets: `websocket.privateMsg.{recipient}.{sender}` | ||
| - Uses `simpMessagingTemplate.convertAndSendToUser()` for targeted delivery | ||
| - History retrieved by querying both sender→recipient and recipient→sender Sets | ||
|
|
||
| **User Session Management:** | ||
| - Online users stored in Redis Set `websocket.onlineUsers` | ||
| - Accessible across all instances for consistent state | ||
| - WebSocket session attributes track username | ||
|
|
||
| **Horizontal Scaling:** | ||
| - Stateless application instances - all state in Redis | ||
| - No direct inter-instance communication | ||
| - New instances auto-join cluster by subscribing to Redis channels | ||
|
|
||
| ## Development Notes | ||
|
|
||
| - **Lombok** is used - ensure IDE has Lombok plugin installed | ||
| - **FastJSON** (alibaba) for JSON serialization | ||
| - Don't save compiled JS/d.ts files in @lib path (per global instructions) | ||
| - Redis must be running for cluster mode; single mode works without Redis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These extra blank lines are unnecessary and reduce code readability. Standard Java conventions, like the Google Java Style Guide, recommend only a single blank line between the last import statement and the class definition. The original code already had one blank line, so these additions are superfluous.