'One to One chat using socket.io in Nestjs

Please let me know if the code I have written for one-to-one chat is correct? and how do I know that messages are emitting from the backend here I am assigning childid to the socket and emitting a message to that childid

@WebSocketGateway({cors: {
      origin: "*",
    },
})
export class MessagesGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect{
  @WebSocketServer()
  server:Server
  private logger: Logger = new Logger('ChatGateway');
  constructor(private readonly messagesService: MessagesService) {
  }
  handleDisconnect(client: any) {
    this.logger.log(`Client disconnected: ${client.childid}`)
  }
  afterInit(server: Server) {
    this.logger.log(`init`);
  }

  handleConnection(client: Server, ...args: any[]) { 
      client.childid = client.handshake.query.childid;
    client.join(client.childid)
    this.logger.log(`Client connected: ${client.childid}`);
  }

  @SubscribeMessage('send_message')
  async handleEvent(@MessageBody() createMessageDto: CreateMessageDto,
  @ConnectedSocket() client: Socket) {
    client.to(createMessageDto.to).to(client.childid).emit("send_message", {
        message: createMessageDto.message,
        from: client.childid,
        to: createMessageDto.to,
    });
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source