PHP code example of marko / pubsub-redis

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

    

marko / pubsub-redis example snippets


use Marko\PubSub\Message;
use Marko\PubSub\PublisherInterface;
use Marko\PubSub\SubscriberInterface;

// Publishing
$publisher->publish(
    channel: 'user.42',
    message: new Message(
        channel: 'user.42',
        payload: json_encode(['text' => 'Hello!']),
    ),
);

// Channel subscription
$subscription = $subscriber->subscribe('user.42');

foreach ($subscription as $message) {
    $data = json_decode($message->payload, true);
}

$subscription->cancel();

// Pattern subscription (Redis-only feature)
$subscription = $subscriber->psubscribe('user.*');

foreach ($subscription as $message) {
    // $message->pattern contains the matched pattern
    $data = json_decode($message->payload, true);
}

return [
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'password' => null,
    'database' => 0,
];

public function publish(string $channel, Message $message): void;

public function subscribe(string ...$channels): Subscription;   // Redis SUBSCRIBE
public function psubscribe(string ...$patterns): Subscription;  // Redis PSUBSCRIBE

public function getIterator(): Generator; // yields Message
public function cancel(): void;