what i did was create a new Helper class file (Javascript Class), then added all the Colyseus functionalities and listeners and everything there. so I can store the room
in a variable there like this.room
which can be accessed from any other component on my whole application.
class ActivityRoom {
constructor() {
this.client = new Colyseus.Client(clientUrl);
}
connect = async (username, rating, photoUrl) => {
this.name = username;
this.rating = rating;
this.photoUrl = photoUrl;
try {
const room = await this.client.joinById('AR1', {
username,
rating,
photoUrl
});
this.room = room;
this.sessionId = room.sessionId;
this.Room_Listeners();
} catch (error) {}
console.error(error);
}
};
leaveRoom() {
this.room.leave();
}
}
then call the leaveRoom
like ActivityRoom.leaveRoom
from any other Component i want.