GitHub | Documentation | Become a Patron

Reference room by room ID



  • Hi! I was wondering, is it possible to reference a room from room ID, say send message to all clients in room?
    I have different classes for state, entites, players etc which don't have reference to the parent room class and therefore can't use 'send' method within those classes.

    Thanks!


  • administrators

    Hi @veggis,

    You may store the room reference on those specific entities by using @nosync.

    Example:

    import { Room, Client, nosync } from "colyseus";
    
    export class Player {
      x: number;
      y: number;
    
      @nosync client: Client;
      @nosync room: Room;
      
      constructor (client: Client, room: Room) {
        this.client = client;
        this.room = room;
      }
    
      sendSomething (data: any) {
        this.room.send(this.client, data);
      }
    }
    

    This way, the client and room properties won't be synched to the client-side.

    Hope this helps. Cheers!



  • This is exactly what I was looking for! Thanks alot @endel



  • @endel Sorry to bother you, but could you give an example with js?

    I'm using babel to transcribe decorators, which is working but not for variables.



  • After some digging around in the docs I got it. Defining objects non-enumerable does the trick.



  • @veggis have you a sample in JS with non enumerable property?



  • @lezanardi

    function setEnumerable(object) {
        let private = {};
    
        for (let key in object) {
            Object.defineProperty(private, key, {
                value: object[key],
                enumerable: false,
                writable: true
            });
        }
    
        return private;
    }
    

    Will return the passed object with all the items non-enumerable.