-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
BUG: Emitting to specific socket IDs - won't emit to itself #3220
Comments
Unfortunately, I cannot edit the post, but I am using Socket.io version 2.0.3 |
I have the same question while running this code: |
According to the documentation, Two solutions:
@blackfish-wu |
According to the [documentation] (https://socket.io/docs/emit-cheatsheet/), it also says:
This is confusing, it is assumed that this command will send a message to any specific socketid. Maybe update the cheatsheet to use "io.to" instead of "socket.to" in this instance. Had the same confusion with a different part of developing my private messaging script the other day until I again realized I had to remember this issue. However, darrachequesne's solution of using Question for darrachequesne If there is a very large number of socket connections, is there a performance difference for using "io.to" vs "socket.to" when emitting to individual socket ids? |
The previous version was confusing, as `socket.to(socket.id).emit()` does nothing. Fixes #3220
The previous version was confusing, as `socket.to(socket.id).emit()` does nothing. Fixes #3220
@peppies you're absolutely right, thanks for the explanation! I updated the documentation in #3232. Regarding the performance, both methods actually call the same code: https://github.com/socketio/socket.io/blob/master/lib/namespace.js#L234-L237 this.adapter.broadcast(packet, {
rooms: rooms,
flags: flags
}); https://github.com/socketio/socket.io/blob/master/lib/socket.js#L170-L173 this.adapter.broadcast(packet, {
except: [this.id],
rooms: rooms,
flags: flags
}); |
@darrachequesne ack not working when emitting to specific socket. ex: io.to(socketId).emit(MESSAGE_TYPE, data, function(
error,
resposne
) {
console.log({ error, resposne });
}); when sending with ack function, message not sent. how to solve it? Thank you. |
The previous version was confusing, as `socket.to(socket.id).emit()` does nothing. Fixes socketio#3220
Greetings,
I noticed a bug today, trying to do something simple, took a few hours to realize it was a bug. Let's say a user has multiple tabs open in their browser, and then they turn their "Away" status to "Online":
Client side:
Server side:
The server emits a "test" response back to all of the sockets (browser tabs) except the originating socket (tab). In my script, the "Online" status gets updated in all of the user's browser tabs - except the one tab where they actually set their online status (and fires off the socket emission to the server).
The only solution would be to use socket.emit('test', 'test') if the originating socket == sockid. That additional logic on the server side should not be necessary and should work using socket.to(sockid).emit. Therefore this is a bug.
Thanks
Kind regards
Ryan
Edit by @darrachequesne: style
The text was updated successfully, but these errors were encountered: