How to send message to client by client ID

Hi everyone.
I am new in Colyseus. I am learning to create a simple game.
All players will join only one room - Lobby
Then choose one in other players to invite to dule ( 1 vs 1 game)
If the invited player accepts, other players will see this two players are marked "Duling"
Then this two players will moved to Game screen to dule.
But i have problem in two steps

  1. How to send message to chosen player?
    I am going to use foreach to check all clients in the room with chosen player client ID. But i think it is bad solution.
  2. I don't know one client can connect to many room or not.
    Because as i understood, these two players will be moved to Game room that only has 2 players. But i still want to keep their name and status in Lobby screen.
    What is best solution?
    Thanks you so much.

hi @deadwind88, welcome!

yes, clients can join multiple rooms. each room connection has its own websocket connection.

you'd need exchange messages between client and server during the Lobby room.

once your custom "handshake" between two clients has established on Lobby handler, you can send a "dule code" to the two players, and make the players join the same "dule" session.

client.join("dule", { code: "bothPlayersShouldJoinThisCode" });

on Dule room handler, you should only allow clients with the same dule code do join toguether:

maxClients = 2;

onInit (options) {
  this.code = options.code;
}

// on dule room handler
requestJoin(options) {
  return this.code === options.code;
}

I haven't tested the code, you can play around and modify as you see fit! Hope this helps, cheers!

@endel Thank you so much.
But i still not understand how to send the code to player who is invited.
For example:
Player A login, and see this lists
B, C, D
A want to invite C to duel, A send a message to Server "Hey, I want to make a duel with C"
I don't know how to make Server to send message to C "Hey A want to make a dule with C" and C can accept or not.
This is my code

// When a client sends a message
    onMessage (client, message) {
        switch (message.action) {
            //A send request to invite C to make a duel
            case 'request_dule':
                let guestId = message.data.guest_id;
                let guest = this.state.players[guestId];
                //find a client has id equal with C's client id
                for (let i = 0; i < this.clients.length; i++) {
                    if (this.clients[i].id == guest.client_Id) {
                        //send request to C's client and wait C accepting
                        this.send(this.clients[i], {action: 'request_dule', data: {host: client.id}});
                        break;
                    }
                }
                break;
            default:
                break;
        }
    }

Looking forward your advice. Thanks

@deadwind88 I think I can help with this.

You need to create an onMessage handler on your client side. Refer to this https://docs.colyseus.io/client-room/#onmessage
so lets say your server code is working fine, in the client, you'd need to extract that message.
ex: onMessage(…) {
if (message.action === "request_dule") {…}
}