Skip to content

Conversation

@twalla26
Copy link
Collaborator

@twalla26 twalla26 commented Nov 12, 2024

관련 이슈 번호

  • 작성자: 송수민
  • 작성 날짜: 2024.11.13

✅ 체크리스트

  • 코드가 정상적으로 작동하는지 확인했습니다.
  • 주요 변경사항에 대한 설명을 작성했습니다.
  • 코드 스타일 가이드에 따라 코드를 작성했습니다.

🧩 작업 내용

  • Redis 설치 및 연결 설정
  • Room 모듈 초기화
  • Redis에 저장할 Room 데이터 설계
  • createRoom 기능 임시 구현

📝 작업 상세 내역

Redis 설치 및 연결 설정

  • 화상 회의로 생성되는 방은 휘발성 데이터라 Redis에 저장하고 관리하기 위해 Redis를 설치했습니다.
  • Redis 모듈을 생성했고, 연결 설정은 다음과 같이 했습니다.
    import { Injectable } from "@nestjs/common";
    import Redis from "ioredis";
    import { redisConfig } from "../config/redis.config";
    
    @Injectable()
    export class RedisService {
        private readonly client: Redis;
    
        constructor() {
            this.client = new Redis({
                host: redisConfig.host,
                port: redisConfig.port,
                password: redisConfig.password,
            });
        }
        getClient(): Redis {
            return this.client;
        }
    }

Room 모듈 초기화

  • 생성한 Room Module 안에는 다음과 같은 파일들이 있습니다(테스트 파일은 미포함).

   ├── room
│   │   ├── room.gateway.ts
│   │   ├── room.model.ts
│   │   ├── room.module.ts
│   │   └── room.service.ts
```
- gateway: 웹소켓 통신 로직
- model: Redis에 저장되는 Room interface 정의
- service: 비지니스 로직 + Redis 접근

Redis에 저장할 Room 데이터 설계

  • Redis에 저장할 Room 데이터는 다음과 같습니다.
    // room/room.model.ts
    export interface Room {
        title: string; // 제목
        createdAt: number; // 회의 시작 시간
        members: string[]; // 회의에 참여한 멤버들
        host: string; // 방장 socket id
    }

createRoom 임시 구현

  • 찬우님과 페어프로그래밍으로 createRoom을 임시로 구현하고 테스트해보았습니다.
    // room/room.service.ts
    async createRoom(title: string, socketId: string) {
        const client = this.redisService.getClient();
        const roomId = generateRoomId();
    
        await client.hset(`room:${roomId}`, { // Redis에 해시로 저장됩니다.
            title: title,
            createdAt: Date.now(),
            members: [socketId],
            host: socketId,
        } as Room);
    
        await client.expire(`room:${roomId}`, 6 * HOUR);
        return roomId;
    }
    // room/room.gateway.ts
    @SubscribeMessage(EVENT_NAME.CREATE_ROOM)
    async handleCreateRoom(client: Socket, data: { title }) {
        try {
            const roomId = await this.roomService.createRoom(
                data.title,
                client.id
            );
            this.server.emit(`room_created`, { roomId });
        } catch(error) {
            console.error(error);
            return null;
        }
    }
    

📌 테스트 및 검증 결과

  • createRoom 결과
    • postman을 요청 보내기
      image
    • redis-cli
      image

💬 다음 작업 또는 논의 사항

  • 목요일까지 달려야 합니다...

@twalla26 twalla26 self-assigned this Nov 12, 2024
@blu3fishez blu3fishez merged commit 0b14b03 into boostcampwm-2024:feature/study-session Nov 12, 2024
@blu3fishez
Copy link
Collaborator

해결되셨다니 다행입니다..

자려고 누웠는데 알림이 떠서 머지 해드렸습니다! (dev 제외 리뷰 없이 머지 가능)

내일 컨디션 회복하고 달려봅시다!!! 🔥🔥🔥

@twalla26 twalla26 changed the title Feature/study session []Redis 설정 및 Room 모듈 초기화 Nov 12, 2024
@twalla26 twalla26 changed the title []Redis 설정 및 Room 모듈 초기화 [Feat]Redis 설정 및 Room 모듈 초기화 Nov 12, 2024
@blu3fishez blu3fishez self-assigned this Nov 13, 2024
@ShipFriend0516
Copy link
Member

ShipFriend0516 commented Nov 13, 2024

@twalla26 고생하셨습니다! host에 소켓 아이디가 들어가는 것 같은데 유저의 id와 유저의 소켓id 가 의미가 겹치지는 않을까요?

@twalla26
Copy link
Collaborator Author

@twalla26 고생하셨습니다! host에 소켓 아이디가 들어가는 것 같은데 유저의 id와 유저의 소켓id 가 의미가 겹치지는 않을까요?

일단 유저의 id는 redis에 저장하지 않을 예정입니다...! 저장할 필요가 없을 거라고 예상되어서 이렇게 구현했는데, 추후에 기능이 확장되면 속성명을 더 자세하게 작성해야겠네욥..!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants