Using await within emit causes payload to be sent to all connected clients
See original GitHub issueYou want to:
- report a bug
- request a feature
Current behaviour
Using async/await
to construct payloads results in a race condition and messages potentially being broadcast to all connections.
Steps to reproduce
Fiddle: https://github.com/tommoor/socket.io-fiddle/tree/async-await-critical-issue
TLDR: Using await
within the payload construction for emit
will cause the message to be sent to all connections instead of the specific room. eg:
io.to('my-private-room').emit('event', {
data: await myDatabaseQuery()
});
Expected behaviour
Data should be sent to the correct room and not leaked to all clients.
Setup
- OS: All
- browser: All
- socket.io version: 2.2.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:21 (3 by maintainers)
Top Results From Across the Web
Client API - Socket.IO
An "error" event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent.
Read more >Socket emitting event multiple times - Stack Overflow
So in your loop you are saying send this everyone but the original socket, and you call that multiple times. What you want...
Read more >Subscriptions in Apollo Server - Apollo GraphQL Docs
payload is the payload of the event that was published. variables is an object containing all arguments the client provided when initiating their...
Read more >Everything you need to know about Socket.IO - Ably Realtime
While data can be sent in a number of forms, JSON is the simplest. ... are using io.emit() to send a message to...
Read more >Redux Fundamentals, Part 6: Async Logic and Data Fetching
So far, all the data we've worked with has been directly inside of our React+Redux client application. However, most real applications need ...
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 FreeTop 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
Top GitHub Comments
@tommoor Not sure why this issue was highjacked but I’ve been dealing with an issue like this for the past few days.
The problem isn’t with
async/await
directly. Usingasync/await
will work fine as long as you ensure.emit(..)
or.clients(..)
isn’t called elsewhere before the Promise resolves. This is because.to(..)
doesn’t return a new instance, and.emit(..)
and.clients(..)
resetNamespace.prototype.rooms
, which is set by.to(..)
.In your example:
The call order is:
After the first Promise resolves,
io.emit(..)
sends toroomId
and clearsrooms
. So subsequents emits send to every socket in the namespace.The simple fix for this is to move the
await
beforeio.to(..)
like such:This yields a call order of:
Hope this helps
Sometime my app has issue :
Cannot read property 'emit' of undefined
A part of my code :What can I do to resolve this problem ? Thank you !