Reputation: 820
Its a duplicate question, though I want to ask it for more clarification on it.
I want to create a private chat using socket.io. While googling for it, I found 2 solutions:
Suppose my app has millions of active users.
Here's what I want: I have my friend list in mysql db, and when I log in, I want all friends and their status (active or not).
case 1. If I use an array to store all active users, then it works pretty well, but is this a good way to store all users who are connecting to my app in an array?
case 2. If I use the room concept where each of users friend have a unique roomid, then whenever a user logs in, he has to join all those roomid. It also worked for me, but in this case, how do I know if my friend is active or not?
I want to know which of this solution will work for my app which will have millions of user, or is there any other way to solve this.
Upvotes: 1
Views: 2046
Reputation: 382130
I would use a third solution :
If you're using socket.IO pre 1.0, then you can use io.sockets.clients(roomName)
to get the sockets in a room.
With socket.IO 1.0, it's not really clear what will be the cleanest solution to list sockets connected to a room :
For now here's a workaround function :
function roomSockets(roomId) {
var clients = io.sockets.adapter.rooms[roomId],
sockets = [];
for (var clientId in clients) sockets.push(io.sockets.connected[clientId]);
return sockets;
}
Note that it might not work with all adapters so it's, at best, a workaround.
Upvotes: 2