Reputation: 2905
I'm building a chat app that's similar to Facebook Messenger. It should have 2 screens, one display a list of conversations that the user is a part of as well as its last message, and the other is the chat screen of a particular conversation. I searched a bit and found out that on the server side we could allow the client to join multiple rooms at once.
Is this common to join client to multiple rooms upon connection? For example when connection is established I'd query the database to find all conversations of the user, and let him join all of that rooms (each room map to a conversation). This way when the user stays in the conversation list, he can still receive messages coming from any conversations.
Something like this:
io.on("connection", (socket) => {
const userRooms = await db.findUserRooms(socket.userId);
socket.join(userRooms);
});
Does this have any performance impact when for example each users have hundreds of conversations?
Upvotes: 3
Views: 1893
Reputation: 1066
Each socket.io connection does occupy some amount of RAM on server side. as you join rooms it hold some amount of RAM till that socket connection alive.
so you can keep top 10 chats as an active and do socket join for that only. For more than 10 chat you can consider as an archive you can join that room when you receive message for that. so that way it don't occupy RAM.
However you need to consider plenty amount of RAM for the server as concurrent users increases load on RAM also increases.
Upvotes: 0
Reputation: 354
According to the Socket.io Docs, the way to join multiple rooms is the way you describe in your question, i.e. socket.join(["room1", "room2", ...])
.
The impact on performance depends on what type of application you're running, and the capabilities of your machine and network.
As rooms are essentially just a server-side map of socket ids, the performace impact of rooms themselves aren't that noticable on the server side.
The performance impact of loading the rooms from your database largely depends on your database, and its capabilities and network.
As I'm not sure what database you're using, I'm unable to provide any information regarding specifics, but most databases should be able to return the rows in a couple of seconds.
Upvotes: 3