socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

A socket cannot emit to itself

Open anestv opened this issue 9 years ago • 4 comments

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 avatar Jun 13 '17 16:06 anestv

@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');

darrachequesne avatar Jun 13 '17 19:06 darrachequesne

Yes, but that would emit to everyone. I want to send it only to that socket.

anestv avatar Jun 17 '17 09:06 anestv

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.

darrachequesne avatar Jun 19 '17 19:06 darrachequesne

Closed due to inactivity, please reopen if needed.

darrachequesne avatar Aug 25 '17 20:08 darrachequesne