PHP code example of huangzonglong / pubsub

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

    

huangzonglong / pubsub example snippets




$client = new Pubsub\PubSubClient('localhost:9900', [
    'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);


$Identity = new \Pubsub\Identity();
$Identity->setClientId($clientId);

$Subscription= new \Pubsub\Subscription();
$Subscription->setTopic($topic);

$SubscribeRequest = new \Pubsub\SubscribeRequest();
$SubscribeRequest->setSubscription($Subscription);
$SubscribeRequest->setIdentity($Identity);

list($sub, $status) = $client->Subscribe($SubscribeRequest)->wait();

if($status->code == 0){
    $responses = $client->Pull($Identity)->responses();
    foreach ($responses as $pull) {
        var_dump($pull->getTopic());
    }
}


//发布测试
$msgArr = ["内容1","内容2"];
$message = [];
foreach($msgArr  as $v){
    $message[] = (new \Pubsub\Message())->setTopic($topic)->setPayload($v);
}

$public = new \Pubsub\PublishRequest();
$public->setMessages($message);
$public->setTopic($topic);
list($respone, $status) = $client->Publish($public)->wait();