Real-time Chat System
Relevant source files
The following files were used as context for generating this wiki page:
- src/main/java/org/example/coding_convention/chat/controller/ChatController.java
- src/main/java/org/example/coding_convention/chat/service/ChatService.java
- src/main/java/org/example/coding_convention/websocket/chat/ChatMessageDto.java
- src/main/java/org/example/coding_convention/chat/model/Chats.java
- src/main/java/org/example/coding_convention/websocket/WebSocketController.java
- src/main/java/org/example/coding_convention/chat/model/ChatsDto.java
- src/main/resources/data.sql
- README.md
The Real-time Chat System in PhanesEditor is designed to facilitate instant communication between project members directly within the editor environment. It utilizes WebSocket protocols to provide low-latency message delivery and a REST API for persistent message storage, ensuring that chat history is maintained across sessions.
The system is strictly scoped to the project level; only authorized project members can send and receive messages within a specific project's chat channel. This integration allows for seamless collaboration while users are concurrently utilizing other real-time features like the shared code editor.
Sources: README.md:15-18, README.md:43-46
System Architecture
The chat system employs a dual-path architecture combining asynchronous WebSocket communication for live updates and synchronous REST endpoints for data persistence.
Communication Flow
- WebSocket Path: Used for broadcasting messages to all active participants in a specific room. The
WebSocketControllerhandles messages mapped to/chat/{roomId}and broadcasts them to/topic/chat/{roomId}. - REST Path: Used for saving chat history to the database. The
ChatControllerhandles the/chat/registerendpoint to persist messages.
The following diagram illustrates the interaction between the client, the WebSocket handler, and the persistence layer:
Data Models and Schema
The system relies on a structured data model to link messages to specific users and projects.
Database Schema
The chats table stores the persistent history of communication. It maintains foreign key relationships with both the project and users tables.
| Field | Type | Constraints | Description |
|---|---|---|---|
| idx | INT | PRIMARY KEY, AUTO_INCREMENT | Unique identifier for the chat record |
| project_id | INT | FOREIGN KEY (project.idx) | The project context of the message |
| user_id | INT | FOREIGN KEY (users.idx) | The author of the message |
| message | TEXT | NOT NULL | The actual text content |
| sent_at | DATETIME | NOT NULL | Timestamp of when the message was sent |
Sources: src/main/resources/data.sql:128-137, src/main/java/org/example/coding_convention/chat/model/Chats.java:22-38
Relationship Diagram
The following ER diagram shows how the Chat entity relates to other core components of the system:
API Implementation
WebSocket Endpoints
The WebSocketController manages real-time message routing using Spring's SimpMessagingTemplate.
- Destination:
/chat/{roomId} - Broadcast Topic:
/topic/chat/{roomId} - Logic: Upon receiving a message, the system injects the sender's username (from the
Principal) and the current server time into the DTO before broadcasting.
Sources: src/main/java/org/example/coding_convention/websocket/WebSocketController.java:21-25, src/main/java/org/example/coding_convention/websocket/chat/ChatMessageDto.java:16-20
REST Endpoints
The ChatController provides a secure way to register and persist chat messages.
| Endpoint | Method | Description | Payload |
|---|---|---|---|
/chat/register | POST | Saves a message to the database | ChatsDto.ChatRegister |
Sources: src/main/java/org/example/coding_convention/chat/controller/ChatController.java:24-29
Data Transfer Objects (DTOs)
The system uses specialized DTOs for different phases of the message lifecycle.
// Used for saving messages via REST API
public static class ChatRegister {
private Integer projectId;
private Integer userId;
@NotBlank(message = "채팅 내용은 공백을 허용하지 않습니다.")
private String message;
}
// Used for broadcasting via WebSocket
public class ChatMessageDto {
private Integer id;
private String message;
private String username;
private String time;
}
Sources: src/main/java/org/example/coding_convention/chat/model/ChatsDto.java:33-40, src/main/java/org/example/coding_convention/websocket/chat/ChatMessageDto.java:10-14
Security and Access Control
Real-time chat access is strictly governed by project membership:
- Authentication: Users must be authenticated, as evidenced by the use of
@AuthenticationPrincipalandPrincipalin controllers. - Authorization: The system restricts message transmission to members of the specific project channel.
- Validation: The
ChatRegisterDTO uses@NotBlankto ensure message integrity.
Sources: README.md:43-46, src/main/java/org/example/coding_convention/chat/controller/ChatController.java:25, src/main/java/org/example/coding_convention/websocket/WebSocketController.java:22
Summary
The Real-time Chat System provides a robust communication layer for PhanesEditor. By separating the real-time broadcast logic (WebSocket) from the persistence logic (REST/JPA), the system ensures high performance during active conversations while maintaining a complete audit trail of project discussions in the MariaDB database.