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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With