nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.89k stars 7.56k forks source link

@WebSocketServer() server -> server is null #1244

Closed Root-Control closed 5 years ago

Root-Control commented 5 years ago

I'm submitting a...


[ ] 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.

Current behavior

Gateway @WebSocketServer() server example is returning "null", then i can't emit messages for client

Expected behavior

I created the native socket io variable, but this variable is null and doesn't works

Minimal reproduction of the problem with instructions

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' }); } }

What is the motivation / use case for changing the behavior?

Environment


Nest version: 5.1.0


For Tooling issues:
- Node version:  8.9.0
- Platform:  Windows

Others:

BrunnerLivio commented 5 years ago

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

Root-Control commented 5 years ago

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

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' });
  }
BrunnerLivio commented 5 years ago

@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.

kamilmysliwiec commented 5 years ago

Thanks @BrunnerLivio for helping with this issue. The second part is not related to Nest itself.

tobinbc commented 5 years ago

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.

lock[bot] commented 4 years ago

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.