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

[socket.io] SubscribeMessage / handleConnection fires two events #732

Closed Loriot-n closed 6 years ago

Loriot-n commented 6 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

WebSocketGateway methods fire twice for each events / connection Note sure if this is expected but this could be problematic in many cases

Expected behavior

Fire only one event

Minimal reproduction of the problem with instructions

server


@WebSocketGateway()
export class SampleWsGateway {
    @WebSocketServer() server

    @SubscribeMessage("events")
    onEvent(client, data: any): WsResponse<any> {
        console.log("received message on [events], data = ", data)
        return data
    }

    handleConnection(client) {
        console.log("connection to socket... token = ", client.handshake.query.token)
    }
}

client : based on sample-2

        const socket = io('http://localhost:3000', {
            query: {
                token: "sampleToken"
            }
        });
        socket.on('connect', function () {
            console.log('Connected');
            socket.emit('events', { test: 'test' });
        });
        socket.on('events', function (data) {
            console.log('event', data);
        });

Logs from the server :

connection to socket... token =  sampleToken
connection to socket... token =  sampleToken
received message on [events], data =  { test: 'test' }
received message on [events], data =  { test: 'test' }

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

Environment


Nest version: 5.0.1


For Tooling issues:
- Node version: 8.9.4  
- Platform:  

Others:

BorntraegerMarc commented 6 years ago

Do you maybe have 2 Gateways with the same namespace configuration? (or maybe no namespace configured at all)

Loriot-n commented 6 years ago

Indeed, I didn't configure any namespaces. But this is the only WebSocketGateway in my application 🤔 Configuring a namespace (even if I don't need one) could resolve the issue?

BorntraegerMarc commented 6 years ago

I doubt it. I had the same issue but 2 gateways on the same namespace

Loriot-n commented 6 years ago

Sorry, I made a mistake, in my app.module.ts I imported my ws.gateway and my ws.module So it was instantiated twice ! Thanks for the help @BorntraegerMarc

RDeluxe commented 5 years ago

I'm actually encountering the same issue. In app.module.ts :

import { Module } from '@nestjs/common';
import { ConnectionsModule } from './connections/connections.module';

@Module({
  imports: [ConnectionsModule],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { ConnectionsGateway } from './connections.gateway';

@Module({
  providers: [ConnectionsGateway],
})
export class ConnectionsModule {}

And ConnectionsGateway implements OnGatewayConnection, without namespace.

The connection event is getting fired twice. If I provide a namespace, the event is fired only once. Not sure if it's me, or a bug in the package while not using a namespace

1ncounter commented 5 years ago

I'm actually encountering the same issue. but it is not resolved.

lock[bot] commented 5 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.