Search code examples
websocketchatratchet

[Websocket][Chat] - How do I find out if a message was send "from client" or "from other user"


The folowing method (check video at this Link) is described in php form if in a userchat app the message is send "from the client" or "from any other user".

       public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, o  == 1 ? '' : 's');
    
        $data=json_decode($msg,true);

        foreach($this->$clients as $client){
        if ($from == $client){
            $data['from']='Me';
        }else{
            $data['from']=get_current_user_id();    
        }
        
        $client->send(json_encode($data));
        
    }

I want do detect the above procedure without calling this function in php. Is this possible to do it via javascript within websockets code for example in a package.json file?

I want to detect if the message was send or recieved from the socket, because I have to arrange them left or right, according to who was send the message - the client user or another.

Thanks in advance for your support

J.


Solution

  • In the body of the conn.onmessage function you can be sure that it is a message received from the server.