PHP code example of zeus / pusher

1. Go to this page and download the library: Download zeus/pusher 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/ */

    

zeus / pusher example snippets


use Zeus\Pusher\AbstractSocketClientHandler;
use Zeus\Pusher\SocketServer;


class ClientHandler extends AbstractSocketClientHandler
{


    #[\Override]
    public function run(): void
    {
        $this->sendTo()->everyone($this->getMessage());
    }
}


$socketServer = new SocketServer(ClientHandler::class);
$socketServer->serve('127.0.0.1', 8080);


class Handler extends AbstractSocketClientHandler
{

    #[\Override]
    public function run(): void
    {

        $this->join('it.frontend', $this->getClient());
        $this->join('it.backend', $this->getClient());

        $this->sendTo()->channel('it.*', 'hello world');


    }
}


$socketServer = new SocketServer(Handler::class);
$socketServer->serve('127.0.0.1', 8080);


class Handler extends AbstractSocketClientHandler
{

    #[\Override]
    public function run(): void
    {
      //get all channels
       $channels=$this->getChannels();
       
       //get all clients of the public channel
       $clients=$this->findChannel('public')->getClients();
       foreach($clients as $client){
           $ip=$this->getRemoteHost($client);
           if('x.x.x.x'===$ip){
               $this->disconnect($client);
           }
       }
       
    }
}


$socketServer = new SocketServer(Handler::class);
$socketServer->serve('127.0.0.1', 8080);


class Handler extends AbstractSocketClientHandler
{

    #[\Override]
    public function run(): void
    {
        $this->leave('it.backend', $this->getClient());
        $this->hasJoin('it.backend', $this->getClient());
        $this->hasChannel('it.backend');
        $this->findChannel('it.backend');
        $this->getChannels();
        //and more
    }
}

$this->leave('it.*');


/**
 * This is a PHP code that extends
 * the AbstractSocketClientHandler class
 * and implements the run() method.
 * It checks if the received message is in JSON format,
 * joins the specified channel, and sends the message to that channel.
 */

class Handler extends AbstractSocketClientHandler
{

    /**
     * @throws JsonException
     */
    #[\Override]
    public function run(): void
    {
        if ($this->isMessageJson()) {

            $this->join(
                $this->getJsonValue('channel'),
                $this->getClient()
            );

            $this->sendTo()->channel(
                $this->getJsonValue('channel'),
                $this->getJsonValue('message')
            );
        }
    }
}


$socketServer = new SocketServer(Handler::class);
$socketServer->serve('127.0.0.1', 8080);

use Zeus\Pusher\SocketClient;                                     
                                                                  
                                                                  
$socketClient = new SocketClient('0.0.0.0', 8080);                
                                                                  
$socketClient->sleep(1);                                          
                                                                  
$message=json_encode([
    'channel'=>'backend',
    'message'=>'a new backend developer has applied'
    ]);    
         
$socketClient->send($message);                                    
                                                                  
return $socketClient->read();                                     

class Handler extends AbstractSocketClientHandler
{

    #[\Override]
    public function run(): void
    {

        //send yourself a message
        $this->sendTo()->client($this->getClient(),'Hello, world');
        //send it to everyone except himself
        $this->sendTo()->exceptClient($this->getClient(),'Hello world');
    }
}


$socketServer = new SocketServer(Handler::class);
$socketServer->serve('127.0.0.1', 8080);

class Handler extends AbstractSocketClientHandler
{

    /**
     */
    #[\Override]
    public function run(): void
    {
        $this->sendTo()->id($this->getId(), 'hello');
    }
}


class Handler extends AbstractSocketClientHandler
{

    /**
     */
    #[\Override]
    public function run(): void
    {
        $this->sendTo()->route('/chat', 'hello world');
    }
}


$socketServer = new SocketServer(Handler::class);
$socketServer->serve('0.0.0.0',8080);

class Handler extends AbstractSocketClientHandler
{

    /**
     */
    #[Override]
    public function run(): void
    {
        $this->hasRoute('/chat');//Is there any client connecting via /chat route?
        $this->isRoute('/chat'); //Is the current client connected via the /chat route?
    }
}


$socketServer = new SocketServer(Handler::class);
$socketServer->serve(
  '0.0.0.0',
   8080
);


class Handler extends AbstractSocketClientHandler
{

    /**
     */
    #[\Override]
    public function run(): void
    {
        $host=$this->getRemoteHost();
        if('x.x.x.x'===$host){
            $this->disconnect($this->getClient());
        }
    }
}


class Handler extends AbstractSocketClientHandler
{

    /**
     */
    #[\Override]
    public function run(): void
    {
        //type 1 using 
        
        $status=$this->getStatus();
        
        //type 2 using
        
        $clients=$this->findChannel('it.backend')->getClients();
        foreach($clients as $client){
            $status=$this->getStatus($client);
            print_r($status);
        }
      
    }
}

### More



class Handler extends AbstractSocketClientHandler
{

    /**
     */
    #[\Override]
    public function run(): void
    {
        Send::method(
            'test',
            static fn(Socket $client) => socket_write($client, Message::encode('test'))
        );


        Channel::method(
            'empty',
            static fn() => $this->clients=[])
        );

        $this->sendTo()->test($this->getClient());
        $this->findChannel('it.backend')->empty();
    }
}
console
php server.php