*클라이언트 코드

스크린샷 2023-08-28 오전 11.06.01.png

npm package 설치

npm i --save @nestjs/websockets @nestjs/platform-socket.io socket.io

gateway.ts 전체 코드

스크린샷 2023-08-28 오전 10.27.10.png

@WebSocketGateway(+process.env.SOCKET_PORT, {
  cors: true,
  namespace: 'TicketNest-socket',
})
import { Server } from 'socket.io';

export class BookingGateway {
  @WebSocketServer()
  server: Server;

  passOrderCountToQueue(userId: string, order: number) {
    console.log('userId:', userId, 'order:', order);
    this.server
      // .to(userId)
      .to('common-room') // common-room 이란 룸에 메세지 보냄. 해당 room에 있는 유저들에게만 브로드 캐스팅 됨.
      .emit('waiting', `${userId}님의 대기 순서는 ${order}번 입니다`);
  }
  @SubscribeMessage('TicketNest-socket')
  async passOrderToClient(client: Socket, userId: string) {
    client.join('common-room');
  }
}