Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Loriot-n opened this issue May 25, 2018 · 7 comments
Closed

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

Loriot-n opened this issue May 25, 2018 · 7 comments

Comments

@Loriot-n
Copy link

Loriot-n commented May 25, 2018

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
Copy link

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

@Loriot-n
Copy link
Author

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
Copy link

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

@Loriot-n
Copy link
Author

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
Copy link

RDeluxe commented Feb 13, 2019

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
Copy link

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

@lock
Copy link

lock bot commented Sep 23, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants