0

I know about this question: Send message to specific client with socket.io and node.js

However I tried any of the answers there and I receive the next error:

/Users/razbuchnik/node/routes/sockets.js:13
      io.to["1ZAJEIGhcMP2k6zqAAAA"].emit("socketFromServer", info);
                                    ^

TypeError: Cannot read property 'emit' of undefined
    at Socket.<anonymous> (/Users/razbuchnik/node/routes/sockets.js:13:37)
    at Socket.emit (events.js:159:13)
    at /Users/razbuchnik/node/node_modules/socket.io/lib/socket.js:513:12
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
Raz
  • 1,910
  • 1
  • 15
  • 28
  • Why devote? isnt this a legitimate one? – Raz Jan 11 '18 at 09:13
  • Can you copy paste the error to StackOverflow ? We didn't like to click on links here :P Also, please talk about the other question; please repeat what did you do to get this error. – NatNgs Jan 11 '18 at 09:48
  • Yes, sure I just wanted to make it easier to read. I will edit my question :) – Raz Jan 11 '18 at 10:16

1 Answers1

1

You can user io.to, but first you have to join the room as specified in the documentation. Read more on https://socket.io/docs/rooms-and-namespaces/#joining-and-leaving.

io.on('connection', function(socket){
  socket.join('some room');
});

and only then

io.to('some room').emit('some event');
Dinca Adrian
  • 1,190
  • 11
  • 21
  • So, what I should do, is register the socket.id of the client in the room, and then emit to this room? – Raz Jan 11 '18 at 10:16
  • I have tried what you said, it works very good. Thank you sir, and see, no need to devote my question, since I found an answer and hope others too! – Raz Jan 11 '18 at 10:20