Server :
import {
ConnectedSocket,
MessageBody,
OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit,
SubscribeMessage,
WebSocketGateway,
WebSocketServer
} from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';
import { Logger } from '@nestjs/common';
@WebSocketGateway()
export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer()
server: Server;
private logger: Logger = new Logger('AppGateway');
@SubscribeMessage('msgToServer')
handleMessage(client: Socket, payload: string): void {
this.server.emit('msgToClient', payload);
}
afterInit(server: Server) {
this.logger.log('Init');
}
handleDisconnect(client: Socket) {
this.logger.log(`Client disconnected: ${client.id}`);
}
handleConnection(client: Socket, ...args: any[]) {
this.logger.log(`Client connected: ${client.id}`);
}
}
Client side:
import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';
const socketConfig: SocketIoConfig = {
url: 'http://localhost:3000',
options: {
path: '/'
}
};
Error
GET http://localhost:3000/?EIO=4&transport=polling&t=NhD2AKw 404 (Not Found)
Guys, make sure you’re hitting your nestjs server. If you use angular you might want to configure proxy like that:
After a couple of years, I’m finally found a solution in my infrastructure. I use nginx as reverse proxy, so
UpdateandConnectionheaders must be added:Using a different port it works but using the same port it happens the bug. I have to say that I’m not using Angular so the client framework has nothing to do.
It works for me using :
client Angular 15, “ngx-socket-io”: “^4.4.0”, with this config :
const config: SocketIoConfig = { url: 'ws://localhost:3004', options: { transports: ['websocket'] } };server Nestjs 9, @nestjs/platform-socket.io": “^9.2.1”, “@nestjs/websockets”: “^9.2.1”, with this config :@WebSocketGateway(3004, { transports: ['websocket']})