How to create rooms with nestjs and socket.io

By changing client: Client to socket: Socket you're able to use the socket object you are used to when using socket.io.

Here is the edited function.

import { Socket } from 'socket.io';
import { WsResponse } from '@nestjs/websockets';

createRoom(socket: Socket, data: string): WsResponse<unknown> {
  socket.join('aRoom');
  socket.to('aRoom').emit('roomCreated', {room: 'aRoom'});
  return { event: 'roomCreated', room: 'aRoom' };
}

With the latest Nest JS update you can use this code where the room name can be sent from the front-end and it will passed on to the 'data' variable:

@SubscribeMessage('createRoom')
  createRoom(@MessageBody() data: string, @ConnectedSocket() client: Socket) {
    client.join(data, err => {
      if (err) {
        this.logger.error(err);
      }
    });
  }