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
67.73k stars 7.63k forks source link

Can't emit messages to client using WsAdapter #3431

Closed hokie closed 4 years ago

hokie commented 4 years ago

Bug Report

Current behavior

Using WsAdapter, I am successfully connecting to the WebSocketGateway from the client, but messages aren't being emitted when calling emit() from the handleConnection() function. I am also able to successfully disconnect from the gateway using client. No errors are being thrown from either the client or Nest app.

Input Code

main.ts

import { NestFactory } from '@nestjs/core';
import { WsAdapter} from "@nestjs/platform-ws";
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useWebSocketAdapter(new WsAdapter(app));
  await app.listen(5555);
}
bootstrap();

test.gateway.ts

import {
  OnGatewayConnection,
  OnGatewayDisconnect,
  WebSocketGateway,
  WebSocketServer,
  SubscribeMessage, OnGatewayInit,
} from '@nestjs/websockets';
import {Server} from "ws";

@WebSocketGateway(10111, {namespace: 'test'})
export class TestwsgatewayGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect{
  @WebSocketServer()
  server: Server;

  constructor(){
  }

  handleConnection(client: any, ...args: any[]): any {
    console.log('Connection detected');
    this.server.emit('events', 'this is a test thing to see what happens');
  }

  handleDisconnect(client: any): any {
    console.log('Disconnection detected');
  }

  afterInit(server: any): any {
    console.log('Gateway successfully inited');
  }
}

client harness

/**
 * Open a new WebSocket connection using the given parameters
 */
function openWSConnection(protocol, hostname, port, endpoint) {
    var webSocketURL = null;
    webSocketURL = protocol + "://" + hostname + ":" + port + "/" + endpoint;
    console.log("openWSConnection::Connecting to: " + webSocketURL);
    try {
        webSocket = new WebSocket(webSocketURL);
        webSocket.onopen = function(openEvent) {
            console.log("WebSocket OPEN: " + JSON.stringify(openEvent, null, 4));
        };
        webSocket.onclose = function (closeEvent) {
            console.log("WebSocket CLOSE: " + JSON.stringify(closeEvent, null, 4));
        };
        webSocket.onerror = function (errorEvent) {
            console.log("WebSocket ERROR: " + JSON.stringify(errorEvent, null, 4));
        };
        webSocket.onmessage = function (messageEvent) {
            var wsMsg = messageEvent.data;
            console.log("WebSocket MESSAGE: " + wsMsg);
        };

    } catch (exception) {
        console.error(exception);
    }
}

Expected behavior

Expect Nest gateway to emit message to client, and message to be logged to client console.

Environment


Nest version: 6.7.2
            
joeyslack commented 4 years ago

It sounds like you're trying to send messages to connected clients when a connection is established?

Have you tried emitting from the client on your connection handler? client.emit(...) in your handleConnection

hokie commented 4 years ago

@joeyslack - I have tried calling client.emit('test', 'test') from inside my handleConnection function and had no luck. HOWEVER, when I use client.send('test', 'test'), the message is received by my client.

 handleConnection(client: any, ...args: any[]): any {
    console.log('Connection detected');
    client.send('events', 'this is a test thing to see what happens');
  }
yoiang commented 4 years ago

Is this related to #3390

kamilmysliwiec commented 4 years ago

Please, provide a minimal repository which reproduces your issue.

cojack commented 4 years ago

@hokie isn't namespace socket.io related feature?

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.