Home > OS >  node js socket.io problem that will only respond to the last connection
node js socket.io problem that will only respond to the last connection

Time:11-23

Basically, I know that 'socket.io' is a one-on-one communication between the server and the client.

But when I tried to do it myself, it wasn't

  1. Connect to the socket using a chrome browser.
  2. Connect to the same page using the secret tab.

in the same situation as described above If an event occurs on client 1, it works on client 2.

How can I generate an event only on each client?

I connected different rooms to each client.

//server
socket.join(`${socket.id}`)
socket.to(`${socket.id}`).emit('event')

It will not work anywhere.

CodePudding user response:

This method:

socket.to(room).emit(...) 

sends to all sockets in the room EXCEPT the one represented by socket.

If you want to send to everyone in the room, then use this:

io.to(room).emit(...)
  • Related