question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[socket.io] SubscribeMessage / handleConnection fires two events

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

1reaction
Loriot-ncommented, May 25, 2018

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

0reactions
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nestjs - Multiple @WebsocketGatways, life cycle hook is called ...
The handleDisconnect event always (correctly) fired once. ... When using the platform-socket.io adapter the namespaces must be identical.
Read more >
Listening to events | Socket.IO
There are several ways to handle events that are transmitted between the ... Adds a listener that will be fired when any event...
Read more >
Gateways | NestJS - A progressive Node.js framework
Let's create a handler that will subscribe to the events messages and respond to the user with the exact same data. events.gateway.ts. JS...
Read more >
09 - Handling Socket.io Connections in NestJS and Postman ...
In this tutorial we'll show how we can log and track connecting and disconnecting socket. io clients. As a bonus, you'll learn how...
Read more >
Scalable WebSockets with NestJS and Redis - LogRocket Blog
As mentioned, we create two Redis clients with different purposes: ... Libraries like socket.io, which we will be using here, do it for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found