How to generate unique 6 digit game code?

Hey there!
I'm building a game in which there is a game host that makes a game and players that join the game.

I want players to join with a unique 6 digit code that is generated. How can I have a game host create a game with a unique code as well as allow that game code for reuse after the game has ended?

Thanks so much!

It's a bit of a loaded quesiton, and colyseus doesn't really support this out of the box but it's possible.

Without touching colyseus's code, I would probably have the 6 digit code mapped to a pre existing game type you have handlers for- Capture The Flag, King of the Hill, or however you're naming you're handlers. Register as many as them as you think you'll need and make sure they have a unique ROOM ID this is for colyseus, this is NOT the id your host is coming up with. So "Capture The Flag 1" "Capture The Flag 2" etc.

Then what'd I do is basically have another room called something like "RoomCreator"... a more suitable name would probably be "RoomMapper" which the host will first connect to, then from there the host can tell the RoomMapper "Hey, I want to create a Capture The Flag room with the ID "jellitans awesome ctf". Then the room mapper will basically look through the registered CTF handlers and see which ones are not already in use. It will then send back the host to connect to the room "Capture The Flag 1" So your client winds up connecting to "Capture The Flag 1" but the RoomMapper will be able to foward future requests of "jellitans awesome ctf" to "Capture The Flag 1"

Or another approach could be just simply registering a handler upon request. You'll probably run into some issues if you're using a presence. Not 100% sure. But I'm 95% the first method would work well regardless of using a presence or not as long as you have the RoomMapper keep track of connected players correctly.

Awesome, thanks so much for the response.

Is there a way on the server to check the list of current rooms? On the server, I want to check to make sure that the game code isn't being used elsewhere.

If you're talking about the custom game code? That's what you're "RoomMapper" room would basically do.

var ctfRoomInUseLookup = {};
var ctfRoomName = "ctfRoom";
// register Capture the flag rooms
for(var i = 0; i < 10; i++) {
    uniqueCtfRoomName = ctfRoomName + i;
    ctfRoomInUseLookup[uniqueCtfRoomName] = false;
    gameServer.register(uniqueCtfRoomName, CTF)

gameServer.register("RoomMapper", RoomMapper, { ctfRoomInUseLookup })

then inside your RoomMapper class you can keep track of which rooms are in use then you can also have another lookup like

// might need to get from redis or database for each new connection if room doesnt stay persisted
customIdsToRoomId = {
    "custom1": "ctfRoom0",
    "cool_kid_room": "ctfRoom1",

if someone wants to create a room.. theres a lot of ways to do this just going to write some pseudo code real quick

requestCreateRoom(roomType, uniqueId) {
    // room doesnt already exist for unique custom id
    if(!(uniqueId in customIdsToRoomId)) {
         // find room not in use
        for(var key in ctfRoomInUseLookup) {
            if(!(ctfRoomInUseLookup[key]) {
                 // found room that isnt being used
                    client.send({ msg: "CREATE_CUSTOM_GRANTED", roomId: key })
                    ctfRoomInUseLookup[key] = true;
                    customIdsToRoomId[uniqueId] = key;


then basically you can do something like when someone requests to connect to a custom id

if("custom1" in customIdsToRoomId) {
    client.send({ msg: "FOUND_ROOM_REQUESTED", roomId: customIdsToRoomId["custom1"]

Youd need to add logic to manage what happens when a player leaves a room and stuff. Actually on second thought it wouldnt be able to work EXACTLY like my code... since rooms dont persist when theres no more users connected so youd basically have to hack "RoomMapper" room to stay persistent somehow or youd have to use Redis or some sort of database to get and edit the look up maps.

Hope that makes some sense. I really need to get back to my own code now lol


You have been more than helpful. Thank you so much!

Thanks a lot for the insights @visgotti

I was tinkering about this, and I think it's possible to use custom-generated roomId's by mutating it during onInit(), I haven't tested though:


import * as generateId from "nanoid/generate";

class MyRoomHandler extends Room {
    onInit () {
        // this may introduce colliding `roomId`, use at your own risk
        this.roomId = generateId("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ", 6);

Make sure to include nanoid as a dependency.