Ratchet: Sending Message to a Specific Client Without Looping Through Array

Created on 25 Oct 2017  路  11Comments  路  Source: ratchetphp/Ratchet

I'd to send a message to a specific client in Ratchet. Currently, I use:

foreach ($this->clients as $client) { if ($tosendid == $client->resourceId) { $client->send($msg); } }

I wonder if there's a direct way to send a message to the client without looping through the whole array. I don't mind, but I wonder if this method is slow because when you've 10,000 concurrent users, it might be slow to loop through an array just to send message to one user.

question

Most helpful comment

Instead of using an SplObjectStorage to store your clients you can use an array:

class MyClass implements \Ratchet\MessageComponentInterface {
    private $clients = [];

    public function onOpen(ConnectionInterface $conn) {
        $this->clients[$conn->resourceId] = $conn;
    }

    public function msgToUser($msg, $id) {
        $this->clients[$id]->send($msg);
    }
}

All 11 comments

Instead of using an SplObjectStorage to store your clients you can use an array:

class MyClass implements \Ratchet\MessageComponentInterface {
    private $clients = [];

    public function onOpen(ConnectionInterface $conn) {
        $this->clients[$conn->resourceId] = $conn;
    }

    public function msgToUser($msg, $id) {
        $this->clients[$id]->send($msg);
    }
}

Thank you in tons!!!

Sir msgToUser is a new function in ratchet?

No @binodpal, it's part of the example code I posted.

how can I pass $id,

`var websocket_server = new WebSocket("ws://localhost:8080/");
websocket_server.onopen = function(e) {
console.log("Connection established!");
};
websocket_server.onerror = function(e) {
// Errorhandling
}
websocket_server.onmessage = function(e)
{
var json = JSON.parse(e.data);
switch(json.type) {
case 'chat':
$('#msg-area').append(json.msg);
break;
}
}

$(document).on('click', '#submit', function(){
var chat_msg = $('#msginput').val();
var to_id = $('#selected_user').val();

websocket_server.send(
JSON.stringify({
'type':'chat',
'user_id':,
'to_id':to_id,
'chat_msg':chat_msg
})
);
$('#msginput').val('');

});`

@samiklah You must create a unique identifier for each user at the server side and then import it at the client-side as it will enable you to identify every user across the $clients array.

For instance, you can populate an array at your WebSocket Server which maps the unique ID of the user to their resource ID.

Can i get custom User id at onOpen event and store it in an array for comparison.
If so then How?

Can i get custom User id at onOpen event and store it in an array for comparison.
If so then How?

You can use the resource Id to identify a user.

public function onOpen(ConnectionInterface $conn) { $this->clients[$conn->resourceId] = $conn; }

I got this from earlier comment let's say i have a notification that i want to send to a specific group of people one thing in my mind was to set the resource id or some other variable with my user id at onOpen event and onmessage i will send the user id's that i want to send message to with $data and then loop through the available users to achieve it but i can't get my head around how to set the custom user id
More Over for every tab open in same browser the server assigns a different Resource ID.

I got this from earlier comment let's say i have a notification that i want to send to a specific group of people one thing in my mind was to set the resource id or some other variable with my user id at onOpen event and onmessage i will send the user id's that i want to send message to with $data and then loop through the available users to achieve it but i can't get my head around how to set the custom user id
More Over for every tab open in same browser the server assigns a different Resource ID.

Please read my answer to this question at https://stackoverflow.com/questions/55094719/how-do-you-send-data-to-the-server-onload-using-ratchet-websockets/55095014#55095014

I read your solution and i think it might work in my scenario too can you share me the sample code for this cause i am new to this and don't know much about it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DmitryPorotov picture DmitryPorotov  路  3Comments

tasaif picture tasaif  路  6Comments

ThePatzen picture ThePatzen  路  3Comments

piotrchludzinski picture piotrchludzinski  路  5Comments

grappetite-ali picture grappetite-ali  路  5Comments