question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

socket.to(socket.id).emit(...) does not emit to himself

See original GitHub issue

You want to:

  • report a bug
  • request a feature

Current behaviour

We are developing a card game for Android. We have a lobby system. When every player in the lobby is ready the game will start. The client who last send the ready-event triggers the startGame() function, which first generates the cards for all clients and then sends the client’s card deck to them. Following source code is handling the sending to the clients:

dealCards(socket) {
  for (let i = 0; i < this.getNumPlayers; i++) {
    // get playercards out of the card deck
    let cards = [];
    for (let j = i; j <= this.cardDeck.length - (this.getNumPlayers - i); j += this.getNumPlayers) {
      cards.push(this.cardDeck[j]);
    }

    this.players[i].setCards(cards);

    // emit cards to player
    socket.to(this.players[i].getSocketId).emit(EVENT.LOBBY.START_GAME, {
      cards: cards,
    });
  }
}

But the client who triggered startGame() is not receiving the start_game-event. When we emit the event like in the following source code it works:

dealCards(socket) {
  for (let i = 0; i < this.getNumPlayers; i++) {
    [...]

    // emit cards to player
    if (this.players[i].getSocketId === socket.id) {
      socket.emit(EVENT.LOBBY.START_GAME, {
        cards: cards,
      });
    } else {
      socket.to(this.players[i].getSocketId).emit(EVENT.LOBBY.START_GAME, {
        cards: cards,
      });
    }
  }
}

Steps to reproduce

A failing and working testing case can be found here.

  1. start the server npm run client
  2. start some clients in different consoles to fill the ‘lobby’ npm run client
  3. point your browser to http://localhost:3000

After pressing the button Trigger Error`` you should see a log message at client's cosnole but not in the browser sonsole. After pressing the button Trigger Working` you should see a log message in all clients console.

Expected behaviour

When using socket.to(id).emit(...) the event should be emitted to the calling socket too. So in my opinion:

// sending to individual socketid (private message)
socket.to(socket.id).emit('hey', 'I just met you');

should act like:

// sending to the client
socket.emit('hey', 'I just met you');

Setup

  • OS: macOS 10.13.1 (Node v8.9.1), Debian 8 (Node v6.11.3)
  • Android Client: 1.0.0
  • socket.io version: 2.0.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
darrachequesnecommented, Jul 31, 2018

@TimmePfeife I added a note in the documentation: https://socket.io/docs/emit-cheatsheet/

I’m closing this now, please do not hesitate if you see any further improvement: https://github.com/socketio/socket.io-website.

1reaction
gregpalacicommented, Jan 24, 2018

I just figured this out the docs are wrong socket.to(socket.id).emit('hey', 'i just met you') should actually be socket.emit(('hey', 'i just met you') where socket is the parent socket

io.on('connection', function(socket) { 
  const user = socket;  
  user.emit('hey', 'I just met you') 
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.to(socket.id).emit() does not work - Stack Overflow
emit ('event name', 'message') as given in the socket.io documention. I'm using node v6. 11.2, express 4.15.
Read more >
Emit cheatsheet - Socket.IO
WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room // named `socket.id` but the sender.
Read more >
How to manage users in socket.io in Node.js ? - GeeksforGeeks
Approach: First, it is important to note that when a new socket is created, it is assigned a unique Id which is retrieved...
Read more >
socket.io emit to specific socket ids : r/node - Reddit
Store all the connected users id and socketid in a key value pair, in js itself itself or on a fast db like...
Read more >
Everything you need to know about Socket.IO - Ably Realtime
WebSockets are also a browser implementation allowing bi-directional communication, however, Socket.IO does not use this as standard.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found