Skip to content

EthanKlocked/chat_server_lite

Repository files navigation

채팅 서버 프로젝트

실시간 채팅 서버입니다. NestJS와 Rust(socketioxide) 두 가지 구현체를 제공합니다.

기능

  • 실시간 메시지 전송
  • 채팅방 생성 및 참여 (1:1, 그룹)
  • 텍스트 및 이미지 메시지 지원
  • 메시지 읽음 상태 관리

로컬 개발

NestJS 서버

npm install
npm run start:dev  # 포트 3003

Rust 서버

cd chat_server_rust
cargo run  # 포트 3003 (SERVER_PORT 환경변수로 변경 가능)

Docker 배포

환경변수 설정

.env.prod 파일 필요:

APP_NAME = soap_chat
JWT_SECRET = your_jwt_secret
REDIS_HOST_CUSTOM = redis
REDIS_PORT_CUSTOM = 6379
REDIS_PASSWORD = your_redis_password
SERVER_PORT = 3003

NestJS 서버 배포 (기본)

docker-compose up -d --build

Rust 서버 배포

SERVER_BUILD_PATH=./chat_server_rust docker-compose up -d --build

WebSocket 이벤트

연결

WebSocket 엔드포인트: ws://your-domain.com/chat

연결 시 헤더에 토큰을 포함해야 합니다:

{
  "token": "your-auth-token"
}

이벤트

클라이언트 -> 서버

  1. 채팅방 생성
socket.emit('initializeChat', { participants: string[] });
  1. 채팅방 입장
socket.emit('enterChat', { roomId: string });
  1. 메시지 전송
socket.emit('sendMessage', {
  roomId: string,
  type: 'text' | 'image',
  content: string[]
});

예시:

  • 텍스트 메시지: { roomId: "room1", type: "text", content: ["Hello, World!"] }
  • 이미지 메시지: { roomId: "room1", type: "image", content: ["http://example.com/image1.jpg", "http://example.com/image2.jpg"] }
  1. 메시지 읽음 표시
socket.emit('markAsRead', { roomId: string, messageId: string });

서버 -> 클라이언트

  1. 상태 업데이트
socket.on('status', (message: string) => {
	console.log(message);
});
  1. 채팅 목록 수신
socket.on('chatList', (chatList: any[]) => {
	console.log(chatList);
});
  1. 채팅 히스토리 수신
socket.on('chatHistory', (messages: any[]) => {
	console.log(messages);
});
  1. 새 메시지 수신
socket.on('newMessage', (message: any) => {
	console.log(message);
});

에러 처리

서버는 에러 발생 시 다음과 같은 이벤트를 발생시킵니다:

socket.on('error', (error: any) => {
	console.error(error);
});

데이터 구조

메시지 (ChatMessage)

interface ChatMessage {
	id: string;
	senderId: string;
	type: 'text' | 'image';
	content: string[];
	timestamp: Date;
	readBy: string[];
}

주의사항

  • 모든 WebSocket 통신은 인증된 사용자만 가능합니다.
  • 메시지 전송 시 항상 typecontent를 올바르게 지정해야 합니다.
  • 이미지 메시지의 경우, content 배열에 이미지 URL을 포함해야 합니다.

프로젝트 구조

chat_server_lite/
├── src/                    # NestJS 소스
├── chat_server_rust/       # Rust 서버 소스
├── docker-compose.yml      # Docker 배포 설정
├── Dockerfile              # NestJS Dockerfile
├── nginx/                  # Nginx 설정
└── certbot/                # SSL 인증서

서버 비교

항목 NestJS Rust
언어 TypeScript Rust
프레임워크 NestJS + Socket.IO Axum + socketioxide
성능 기준 ~50% 빠름
메모리 ~200-500MB ~20-50MB

About

Realtime chat backend with NestJS and Rust implementations, WebSocket gateways, Redis, and Docker deployment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors