[socket.io] SubscribeMessage / handleConnection fires two events
See original GitHub issueI’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:
- Created 5 years ago
- Reactions:3
- Comments:7
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 @BorntraegerMarcThis 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.