[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Gateway @WebSocketServer() server example is returning "null", then i can't emit messages for client
I created the native socket io variable, but this variable is null and doesn't works
import {
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
WsResponse,
} from '@nestjs/websockets';
import { from, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@WebSocketGateway()
export class EventsGateway {
@WebSocketServer() server;
constructor() {
this.server.emit('testing', { do: 'stuff' });
}
@SubscribeMessage('testing')
sendTesting(client, data) {
client.emit('testing', { testing: 'works' });
}
}
Nest version: 5.1.0
For Tooling issues:
- Node version: 8.9.0
- Platform: Windows
Others:
The @WebSocketServer() property will be assigned after init, not in the constructor
I think it should look something like this (not tested):
import {
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
WsResponse,
OnGatewayInit,
} from '@nestjs/websockets';
import { from, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@WebSocketGateway()
export class EventsGateway implements OnGatewayInit {
@WebSocketServer() server;
afterInit() {
this.server.emit('testing', { do: 'stuff' });
}
@SubscribeMessage('testing')
sendTesting(client, data) {
client.emit('testing', { testing: 'works' });
}
}
More information in the docs under Server chapter
The
@WebSocketServer()property will be assigned after init, not in the constructorI think it should look something like this (not tested):
import { SubscribeMessage, WebSocketGateway, WebSocketServer, WsResponse, OnGatewayInit, } from '@nestjs/websockets'; import { from, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @WebSocketGateway() export class EventsGateway implements OnGatewayInit { @WebSocketServer() server; afterInit() { this.server.emit('testing', { do: 'stuff' }); } @SubscribeMessage('testing') sendTesting(client, data) { client.emit('testing', { testing: 'works' }); } }More information in the docs under Server chapter
Works, server now is not null, but strangely afterInit is not emiting data to ui.
afterInit() {
console.log(this.server);
this.server.emit('testing', { do: 'stuff' });
}
@Root-Control
I do not think this is directly related to Nest anymore. Maybe try to debug your client by creating a minimal socket-io client listener similar to this one, so you can debug where the problem is.
Thanks @BrunnerLivio for helping with this issue. The second part is not related to Nest itself.
I know this is closed but I also had a problem where server was null well after app boot. In my case I had left App.component.ts with the default content, i.e. it was listening on the top level route / and I assume blocking any websocket listeners from instantiating, on any port.
As above ,in the emit I am also not getting any data through, just the event name.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
The
@WebSocketServer()property will be assigned after init, not in the constructorI think it should look something like this (not tested):
More information in the docs under Server chapter