PHP code example of laravie / streaming

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

    

laravie / streaming example snippets




$eventLoop = React\EventLoop\Factory::create();

$chat = new class implements Laravie\Streaming\Listener {
    /**
     * @return array<int, string>
     */
    public function subscribedChannels(): array {
        return ['topic:*'];
    }
    
    /**
     * @param  \Predis\Async\Client  $redis  
     * @return void
     */
    public function onConnected($redis) {
        echo "Connected to redis!";
    }

    /**
     * @param  \Predis\Async\Client  $redis  
     * @return void
     */
    public function onSubscribed($redis) {
        echo "Subscribed to channel `topic:*`!";
    }
    
    /**
     * Trigger on emitted listener.
     *
     * @param  object  $event
     * @param  object  $pubsub
     *
     * @return void
     */
    public function onEmitted($event, $pubsub) {
        // PUBLISH topic:laravel "Hello world"
        
        # DESCRIBE $event
        #
        # {
        #   "kind": "pmessage",
        #   "pattern": "topic:*",
        #   "channel": "topic:laravel",
        #   "payload": "Hello world"
        # }
    }
}

$client = new Laravie\Streaming\Client(
    ['host' => '127.0.0.1', 'port' => 6379], $eventLoop
);

$client->connect($chat);

$eventLoop->run();