GitHub | Documentation | Become a Patron

Joining specific rooms, playing against friends



  • Hello!

    How would you go about letting people join specific rooms? For example, suppose I want to play against my friends; how would I join their room? I saw Federkun's workaround here, but it would be nice if this feature was included in Colyseus.

    If this hasn't been implemented, is there any way I can help? :)


  • administrators

    Hey @ScottyFillups,

    Whilst it's on the framework's roadmap to have public room listing, it's currently possible to share the room id with friends, which allow them to connect directly to it. (Docs: http://colyseus.io/docs/client-room/#properties)

    You'd need to handle this case on your own, though.

    You have a couple of options, such as:

    1. Letting the user set a password for the private room upon creation, and prevent clients from joining without the password on requestJoin.
    class MyRoom extends Room {
      onInit (options) {
        this.password = options.password;
      }
    
      requestJoin (options) {
        return (this.password && options.password == this.password);
      }
    
    }
    
    1. Registering a new handler "private" for private rooms. Fresh clients could join only the "public" by default, while the "private" rooms could have the id shared with friends. On this setup there's a possibility that users can "hack" the JavaScript file, allowing to join the "private" rooms by default instead, but that's unlikely to happen.
    gameServer.register("public", MyRoom);
    gameServer.register("private", MyRoom);
    

    Hope this helps! Cheers!


  • administrators

    One thing I've forgotten to mention is how to connect to the "private" room from the client-side. You'd have this call in your client:

    import { Client } from "colyseus.js";
    
    const client = new Client("ws://...");
    client.join("private", { password: "someUniqueCode" })