GitHub Documentation Patreon donate button

How to influence roomId for new room?



  • Is there a way to choose or influence what the roomId will be when a new instance of a room is created?

    For example, if I do something like

    gameServer.register("chat", ChatRoom).then((handler) => {
      handler.
        on("create", (room) => console.log("room created:", room.roomId)).
        on("dispose", (room) => console.log("room disposed:", room.roomId)).
        on("join", (room, client) => console.log(client.id, "joined", room.roomId)).
        on("leave", (room, client) => console.log(client.id, "left", room.roomId));
    })
    

    and set max clients to 2, when a new client joins the "chat" room then a new room instance gets created with a unique roomId. That's great, but the room IDs are not so friendly and so if I want one player to give his room ID to another so they can play in the same game, I would like friendlier room IDs. Can the server (or better yet the client) somehow influence the room ID either by choosing it directly or providing a suffix or prefix?

    Thanks.


  • administrator

    hey @cmdt-ed, you can override the roomId during onInit():

    // server-side
    onInit(options) {
      this.roomId = options.roomId;
    }
    
    // client-side
    var room = client.join("chat", { roomId: "hello" })
    

    Keep in mind that making sure these values are unique is in your hands this way.



  • Thanks. That sounds great. Since javascript is single-threaded, I should just be able to check the proposed roomId on the server, right?

    Could you point me to the property or method to get the room IDs on the server?

    Thanks.



  • Looking at the code it seems like maybe I should look at this.presence.smembers(roomName) to get a list of all rooms (not just the available ones). Does that sound like the right thing to check to make sure I am not creating a roomId which already exists?


  • administrator

    Hey @cmdt-ed, it's not ideal but you can use that. The Presence API doesn't currently support sismember - which is better for checking that, I'd appreciate if you can send me a pull-request adding it! https://redis.io/commands/sismember

    There's only two Presence implementations now, both can implement this method:

    • LocalPresence: for single-threaded environments link
    • RedisPresence: for multi-threaded environments link

    Cheers



  • OK, I'll look into doing that. I don't have much experience with redis, promises, async and even javascript but I'll see what I can do.

    Thanks.


 

GitHub Documentation Patreon donate button

© 2019 Endel Dreyer