Q: Socket IO and Dynamic Rooms If anyone is experienced with Websockets / Socket IO hopefully you can point me in the right direction.
I made a Discord Clone and I'm trying to optimize it to scale better. Right now when a user sends a message I query the DB for all users part of that server, and emit a message to their specific socket. This is obviously not going to scale well as every message requires a expensive query and lookup in the client list
// Emit messages to only users part of specific server
    // Will only return list of users part of server and active in last 10 minutes
    sqlQuery = `SELECT userservers.user_id FROM userservers 
                  JOIN users ON users.user_id = userservers.user_id AND users.user_last_active > (NOW() - INTERVAL 10 minute)
                  WHERE server_id = ${sql.escape(serverId)}`;
    const users = await sql.query(sqlQuery);
    action = { type: "message", payload: msg };

    // Iterate over users, and find them in clients list
    // Emit over socket only to that user
    users.forEach((user) => {
      clients.forEach((client) => {
        if (client.userId === user.user_id) {
          io.to(client.id).emit(user.user_id, action);
        }
      })
    });

However using Rooms for each Sever would eliminate my need to query the DB. I understand I can do this when the socket server first starts
// Get server list from Mysql DB
servers.forEach((server) => {
    socket.join(server.name);
}

However my issue becomes, when a user create a new server once the application is already running It will not update the list.
I am probably missing some concept on creating dynamic rooms.
EDIT : I am thinking the solution could be that every time a "server" is created, I send a message to the socket server so it can join that "room"

A: 
Right now when a user sends a message I query the DB for all users part of that server

I think you can submit broad cast message to all online users, so instead of forEach client => io.to(clientId)  you can submit broad cast message to all connected users io.emit('some event', { for: 'everyone' });
also I'm wondering why you are creating many servers? you can divide your server into namespaces by using const namespace = io.of('/thisIsASeparateNamespace'); and also you can submit broadcast messages to all users inside this name space by namespace.emit('some event', { for: 'everyone in name space' }); 
So your chat structure can be like this 
Server


*

*Namespaces // for separate chat app / or like slack work spaces 

*Rooms // for group chatting

*ClientID // for one to one

