A socket cannot emit to itself
You want to:
- [x] report a bug
- [ ] request a feature
Current behaviour
Emits sent using socket.to(socket.id).emit( ... ) are not sent.
I have pasted the output of running the fiddle with DEGUG=*
connect XtxCnJ-60yZ9gMZKAAAA
socket.io-parser encoding packet {"type":2,"data":["hello","world"],"nsp":"/"} +7s
socket.io-parser encoded {"type":2,"data":["hello","world"],"nsp":"/"} as 2["hello","world"] +0ms
socket.io:client writing packet {"type":2,"data":["hello","user XtxCnJ-60yZ9gMZKAAAA"],"nsp":"/"} +10ms
socket.io-parser encoding packet {"type":2,"data":["hello","user XtxCnJ-60yZ9gMZKAAAA"],"nsp":"/"} +2ms
socket.io-parser encoded {"type":2,"data":["hello","user XtxCnJ-60yZ9gMZKAAAA"],"nsp":"/"} as 2["hello","user XtxCnJ-60yZ9gMZKAAAA"] +0ms
engine:socket sending packet "message" (2["hello","user XtxCnJ-60yZ9gMZKAAAA"]) +35ms
Steps to reproduce (if the current behaviour is a bug)
See the following fiddle
Basically all you do is socket.to(socket.id).emit('hello', 'world');
Expected behaviour
It should emit to the current socket.
Setup
- socket.io version: 2.0.3
Other information (e.g. stacktraces, related issues, suggestions how to fix)
I think what causes this bug is line 171 at socket.js. A solution would be to not add the socket's id to the except array when the only recipient is that socket.
Also, using io.to(socket.id).emit('hello', 'world'); works as expected and does not cause this bug.
@anestv yep, that's expected: socket.to(<room>).emit('hello', 'world'); is meant to send to anyone in the room but the sender, so in that particular case the message is not sent.
You can simply use socket.emit('hello', 'world');
Yes, but that would emit to everyone. I want to send it only to that socket.
I think you're confusing io.emit, which broadcasts to everyone in the namespace, and socket.emit, which only sends to the given socket. Or am I missing something?
Please see https://github.com/socketio/socket.io/blob/master/docs/emit.md.
Closed due to inactivity, please reopen if needed.