PHP code example of ollyxar / websockets-chat

1. Go to this page and download the library: Download ollyxar/websockets-chat library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ollyxar / websockets-chat example snippets


Ollyxar\WSChat\WSChatServiceProvider::class,

 namespace App;
 
use Generator;
use Ollyxar\LaravelAuth\FileAuth;
// or you can use RedisAuth if you're storing sessions in the Redis-server:
// use Ollyxar\LaravelAuth\RedisAuth;
use Ollyxar\WebSockets\{
    Frame,
    Handler as Worker,
    Dispatcher
};
 
 /**
  * Class Handler
  * @package App
  */
 class Handler extends Worker
 {
     /**
      * Connected users
      *
      * @var array
      */
     protected $users = [];
 
     /**
      * Append connected user
      *
      * @param array $headers
      * @param $socket
      * @return bool
      */
     private function fillUser(array $headers, $socket): bool
     {
         if ($userId = FileAuth::getUserIdByHeaders($headers)) {
             // allow only one connection for worker per user
             if (!in_array($userId, $this->users)) {
                 $this->users[(int)$socket] = $userId;
                 return true;
             }
         }
 
         return false;
     }
 
     /**
      * @param $client
      * @return Generator
      */
     protected function onConnect($client): Generator
     {
         $userName = User::where('id', (int)$this->users[(int)$client])->first()->name;
         yield Dispatcher::async($this->broadcast(Frame::encode(json_encode([
             'type'    => 'system',
             'message' => $userName . ' connected.'
         ]))));
     }
 
     /**
      * @param array $headers
      * @param $socket
      * @return bool
      */
     protected function validateClient(array $headers, $socket): bool
     {
         return $this->fillUser($headers, $socket);
     }
 
     /**
      * @param $clientNumber
      * @return Generator
      */
     protected function onClose($clientNumber): Generator
     {
         $user = User::where('id', (int)@$this->users[$clientNumber])->first();
         $userName = data_get($user, 'name', '[GUEST]');
 
         yield Dispatcher::async($this->broadcast(Frame::encode(json_encode([
             'type'    => 'system',
             'message' => $userName . " disconnected."
         ]))));
 
         unset($this->users[$clientNumber]);
         yield;
     }
 
     /**
      * @param string $message
      * @param int $socketId
      * @return Generator
      */
     protected function onClientMessage(string $message, int $socketId): Generator
     {
         $message = json_decode($message);
         $userName = User::where('id', (int)$this->users[$socketId])->first()->name;
         $userMessage = $message->message;
 
         $response = Frame::encode(json_encode([
             'type'    => 'usermsg',
             'name'    => $userName,
             'message' => $userMessage
         ]));
 
         yield Dispatcher::async($this->broadcast($response));
     }
 }
 
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
bash
php composer.phar 
bash
 php composer.phar 
bash
php artisan websockets-chat:run
bash
php artisan websockets-chat:send "Hello from system!"