<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to send message to client by client ID]]></title><description><![CDATA[<p>Hi everyone.<br />
I am new in Colyseus. I am learning to create a simple game.<br />
All players will join only one room - Lobby<br />
Then choose one in other players to invite to dule ( 1 vs 1 game)<br />
If the invited player accepts, other players will see this two players are marked &quot;Duling&quot;<br />
Then this two players will moved to Game screen to dule.<br />
But i have problem in two steps</p>
<ol>
<li>How to send message to chosen player?<br />
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.</li>
<li>I don't know one client can connect to many room or not.<br />
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.<br />
What is best solution?<br />
Thanks you so much.</li>
</ol>
]]></description><link>http://discuss.colyseus.io/topic/206/how-to-send-message-to-client-by-client-id</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 12:53:33 GMT</lastBuildDate><atom:link href="http://discuss.colyseus.io/topic/206.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 04 Feb 2019 10:01:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to send message to client by client ID on Invalid Date]]></title><description><![CDATA[<p>Hi everyone.<br />
I am new in Colyseus. I am learning to create a simple game.<br />
All players will join only one room - Lobby<br />
Then choose one in other players to invite to dule ( 1 vs 1 game)<br />
If the invited player accepts, other players will see this two players are marked &quot;Duling&quot;<br />
Then this two players will moved to Game screen to dule.<br />
But i have problem in two steps</p>
<ol>
<li>How to send message to chosen player?<br />
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.</li>
<li>I don't know one client can connect to many room or not.<br />
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.<br />
What is best solution?<br />
Thanks you so much.</li>
</ol>
]]></description><link>http://discuss.colyseus.io/post/672</link><guid isPermaLink="true">http://discuss.colyseus.io/post/672</guid><dc:creator><![CDATA[deadwind88]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to How to send message to client by client ID on Mon, 04 Feb 2019 11:05:44 GMT]]></title><description><![CDATA[<p>hi <a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/318">@deadwind88</a>, welcome!</p>
<p>yes, clients can join multiple rooms. each room connection has its own websocket connection.</p>
<p>you'd need exchange messages between client and server during the Lobby room.</p>
<ul>
<li><a href="https://docs.colyseus.io/client-room/#send-data" rel="nofollow">sending messages from client to server</a></li>
<li><a href="https://docs.colyseus.io/client-room/#onmessage" rel="nofollow">onMessage in the client-side</a></li>
<li><a href="https://docs.colyseus.io/api-room/#onmessage-client-data" rel="nofollow">onMessage in the server-side</a></li>
</ul>
<p>once your custom &quot;handshake&quot; between two clients has established on Lobby handler, you can send a &quot;dule code&quot; to the two players, and make the players join the same &quot;dule&quot; session.</p>
<pre><code>client.join(&quot;dule&quot;, { code: &quot;bothPlayersShouldJoinThisCode&quot; });
</code></pre>
<p>on Dule room handler, you should only allow clients with the same dule code do join toguether:</p>
<pre><code>maxClients = 2;

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

// on dule room handler
requestJoin(options) {
  return this.code === options.code;
}
</code></pre>
<p>I haven't tested the code, you can play around and modify as you see fit! Hope this helps, cheers!</p>
]]></description><link>http://discuss.colyseus.io/post/673</link><guid isPermaLink="true">http://discuss.colyseus.io/post/673</guid><dc:creator><![CDATA[endel]]></dc:creator><pubDate>Mon, 04 Feb 2019 11:05:44 GMT</pubDate></item><item><title><![CDATA[Reply to How to send message to client by client ID on Mon, 04 Feb 2019 14:36:17 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/1">@endel</a> Thank you so much.<br />
But i still not understand how to send the code to player who is invited.<br />
For example:<br />
Player A login, and see this lists<br />
B, C, D<br />
A want to invite C to duel, A send a message to Server &quot;Hey, I want to make a duel with C&quot;<br />
I don't know how to make Server to send message to C &quot;Hey A want to make a dule with C&quot; and C can accept or not.<br />
This is my code</p>
<pre><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 &lt; 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;
        }
    }
</code></pre>
<p>Looking forward your advice. Thanks</p>
]]></description><link>http://discuss.colyseus.io/post/674</link><guid isPermaLink="true">http://discuss.colyseus.io/post/674</guid><dc:creator><![CDATA[deadwind88]]></dc:creator><pubDate>Mon, 04 Feb 2019 14:36:17 GMT</pubDate></item><item><title><![CDATA[Reply to How to send message to client by client ID on Invalid Date]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://discuss.colyseus.io/uid/318">@deadwind88</a> I think I can help with this.</p>
<p>You need to create an onMessage handler on your client side. Refer to this <a href="https://docs.colyseus.io/client-room/#onmessage" rel="nofollow">https://docs.colyseus.io/client-room/#onmessage</a><br />
so lets say your server code is working fine, in the client, you'd need to extract that message.<br />
ex: onMessage(…) {<br />
if (message.action === &quot;request_dule&quot;) {…}<br />
}</p>
]]></description><link>http://discuss.colyseus.io/post/675</link><guid isPermaLink="true">http://discuss.colyseus.io/post/675</guid><dc:creator><![CDATA[KG204]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>