Client create room only, but both `onCreate` and `onJoin` are triggered in Server

I try to use H5 client to test the demo. Firstly, client create the room, then click the button to join the room.

client.js

const client = new Colyseus.Client(ws_origin_url);
client.create('chat').then(room =>{
    console.log('room has been created');    
});

playButton.addEventListener('click', ()=>{
  client.join("chat", { mode: "chit-chat" }).then(room => {
    console.log("joined successfully", JSON.stringify(room));
  }).catch(e => {
    console.error("join error", e);
  });
})

chatRoom.js

import { Room } from "colyseus";

export class ChatRoom extends Room {
  private maxClient = 4;
  constructor() {
    super();
  }
  onCreate(options) {
    console.log("chat room created", JSON.stringify(options));
  }

  onJoin(client, options){
    console.log(`client: ${JSON.stringify(client)} has joined this room ${JSON.stringify(options)}` ); 
  }
}

when i request the web page, the console shows.

chat room created {"mode":"chit-chat"}
client: {"sessionId":"BdiirrQTY","readyState":1} has joined this room {}

This means that both onCreate and onJoin are triggered in chatRoom.js. but client hasn't join yet , as i haven't click the playButton.

once i clicked the playButton, the client join the room. in server console, it shows

client: {"sessionId":"u01i6ij3l","readyState":1} has joined this room {"mode":"chit-chat"}

pls notice that the sessionID has changed. Although it is the same client in the same web route.

In Sum, two questions:

  • client only creates the room, but server onCreate and onJoin both are triggered. Is it reasonable? i assume that when the client creates room, only onCreate is triggered; when the client joins the room, then onJoin triggered.

  • The same client, same route, but sessionID is different before and after join the room. why is sessionID changed? can we make it no change because it is the same client?

Very appreciate if anyone can answer.

Hi @sunq0001

client only creates the room, but server onCreate and onJoin both are triggered. Is it reasonable? i assume that when the client creates room, only onCreate is triggered; when the client joins the room, then onJoin triggered.

That is the intended behavior. The “onCreate” is called because the room has been created in the server, and “onJoin” gets called whenever a client joins it. When the client requests to create a room, both are executed.

The same client, same route, but sessionID is different before and after join the room. why is sessionID changed? can we make it no change because it is the same client?

This is also intended. A session is an ephemeral connection between a client and the server. Whenever the client disconnects and connects again, a brand new session is created. If you need to identify a user across multiple sessions you’d need to use authentication.

Hope this helps, cheers!

Hi, @endel
Thanks a lot for your explanation quickly.
It is helpful to clear confusion in my mind.