Sending Data Only To Chosen Users Using Socket.io-node
Solution 1:
Normally you should have for each room a list of connected user and those user all have a client
object that you should have stored somewhere. So when you want to send a message to a specific room, you just have to iterate over the connected user of that room and access their client
object and send the data.
In short, it is possible you just have to send your data to each of the users in the group one-by-one.
Solution 2:
socket.io has a grouping functionality built in
On the socket object for a single connection, like you get passed when a new user connects, you can call .join('roomName') where roomName is any string you want to use to identify the "room", you could use a room name like "profile/14" to create a channel for updates to user #14's profile.
Then on the main io object do something like:
io.sockets.in('profile/14').emit('newComment', {message:'hello'});
The message will go out to all connections that have .join()'d the given room.
Typically I'll have my client emit a "hello" event onConnect that identifies what content the client is interested in subscribing to, and then on the server side my handler for the "hello" event handles .join()'ing the client into whatever rooms are needed
Post a Comment for "Sending Data Only To Chosen Users Using Socket.io-node"