I created a method in chat.gateway.ts to create a chat room:
@SubscribeMessage('createRoom')
async handleCreateRoom(@MessageBody() body: CreateRoomDto, @ConnectedSocket() socket: Socket) {
const token = socket.handshake.headers.authorization.split('Bearer ')[1];
console.log(body);
const chat = await this.chatService.createRoom(body, token);
socket.join(body.name);
this.server.to(body.name).emit('roomCreated', chat);
}
On the client side, there is a form in which the client writes the name of the room. How do I pass this data to the server side and create a room? Until I can figure it out
import io from 'socket.io-client';
import { getDataFromLS } from "../utils/auth";
import {IRoom} from "../types/types";
import api from './axiosClient'
const socket = io('http://localhost:3000');
export class ChatClient {
static async createRoom(roomName: string) {
try {
const token = getDataFromLS();
socket.emit('createRoom', {name: roomName, token: token.accessToken});
socket.on('roomCreated', (room: IRoom) => {
console.log('room created: ' + room);
})
} catch (e) {
console.error('Error creating room:', e);
}
}
}
But this is not a working option, how to do it right?