0

So, here's the thing. I'm writing a small web application using node.js with my partner. In this application, we need to send different data to different clients. Right now, I implemented this demand using scripts like below:

io.sockets.to(ClientId).emit();

Although the code works well, I'm still not sure if it's a proper way to implement the needs, which is, sending different data to different clients.

So, any advice? Thanks a lot!

Kacper Polak
  • 1,411
  • 15
  • 24

1 Answers1

2

You could use socket.io rooms.

socket1.join("room1");
socket2.join("room1");

socket3.join("room2");
socket4.join("room2");

io.to("room1").emit("event", "data");
io.to("room2").emit("event", "different data");

The code above would send "data" to socket1 and socket2 and send "different data" to socket3 and socket4.

More on that here.

Community
  • 1
  • 1
dodov
  • 5,206
  • 3
  • 34
  • 65