Join vs create room

I've been looking at the colyseus-examples repository. All the examples are very instructive. However, in the create-or-join-room example, I'm unable to figure out how the room creation is happening.

Both for joining or creating the room the code in create-or-join-room.html seems to be calling the client.join function. When additional options are being passed isNewRoom is also being set to true. I'm unable to figure out how it's working. When does isNewRoom set to true? I can also see onInit for the room being called before requestJoin.

Could you please explain the flow of the code? I've looked into the code of MatchMaking.ts but haven't figured out the flow yet.


Hi @madki, welcome!

The isNewRoom property is set automatically, internally on MatchMaker.ts.

In the client-side, there's no built-in API for explicitly creating rooms. The server will try to join the best room it can find (based on maxClients / requestJoin()) - and, in case no room is suitable for joining, it will create a new room, and then call requestJoin() on it.

That's why you see onInit() called before requestJoin(). The room needs to be instantiated before requestJoin() is called.

In case a room is found to join, isNewRoom will be false, to determine that the client is joining an existing room.

Hope this helps! Let me know if you have any question. Cheers!

Hi @endel,

Thanks for the quick response and the amazing library!

Thanks for the code references. I have better clarity of the process now. What I'm still trying to wrap my head around is how the create option in the create-or-join-room example is consistently able to create a new room. Once a room has been created shouldn't the second request join the same room? Why isn't the request able to find a suitable existing room? As far as I can see the room created after the first request still satisfies the options for subsequent ones.


@madki I see,

On the 04-create-or-join-room.ts example, you can see that the requestJoin() method is checking for both options.create AND isNewRoom. That means requestJoin() will succeed only if isNewRoom is also true.

requestJoin (options, isNewRoom: boolean) {
    return (options.create)
        ? (options.create && isNewRoom)
        : this.clients.length > 0;

So, during matchmaking, the server will first try to call requestJoin(options, false) in all unlocked spawned rooms, and - if all of the existing rooms return false - it will create a new room and call requestJoin(options, true) as a final attempt. If even the final attempt returns false, that new room will be disposed immediately.

Hope this helps! Cheers!

@endel Now I get it! Thanks so much for the help. I have enough to get started on my project.

Just curious though, why isn't an explicit create call supported? I see it being useful in scenarios where users can create their own game and share it with friends. The rooms can't really be locked when the game starts as the players might change devices during the course of the game. When there are many existing rooms, the creation of new rooms might become expensive. Is it a security concern for the clients to be able to create the rooms, or just not a common scenario?