#socket.io wont emit

7 messages · Page 1 of 1 (latest)

misty abyss
#

hi been trying to make a chat function on my nestjs project but the this.server.emit doesnt seem to be working. this is the code:

import { ChatsService } from './chats.service';
import { CreateChatDto } from './dto/create-chat.dto';
import { OnModuleInit, Request, UseGuards } from '@nestjs/common';
import { ConnectedSocket, MessageBody, SubscribeMessage, WebSocketGateway, WebSocketServer, WsResponse } from '@nestjs/websockets';
import { AccessTokenGuard } from 'src/auth/guard/access-token.guard';
import { WebSocketGuard } from 'src/auth/guard/web-socket.guard';
import { Server, Socket } from 'socket.io'

@WebSocketGateway({
  cors: {
    origin: '*'
  }
})
export class ChatsGateway implements OnModuleInit {
  @WebSocketServer()
  server: Server
  onModuleInit() {
    this.server.on('connection', (socket) => {
      console.log(`Connected ${socket.id}`)
    })
  }
  constructor(private readonly chatsService: ChatsService) { }

  @SubscribeMessage('testing')
  async testing(@MessageBody() data: any, @MessageBody('id') id: number) {
    console.log(data)
    console.log(id)
    this.server.emit('hi', { data: data.text })
    return id
  }

}```

all the console.log is displaying the correct data but i cant seem to get the this.server.emit to work properly(cant get a response back from postman other than the ack)
#

how i remove this

#

kinda solved it

#

stupid mistake actually

#

needed to name the events to 'hi' not 'testing'

#

XD

upper flame