PHP code example of gockets-project / gockets-php

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

    

gockets-project / gockets-php example snippets


use Gockets\Client;
use Gockets\Model\Params;

$host = 'localhost'; // Default value
$port = '8844'; // Default value

$client = new Client(new Params($host, $port));

use Gockets\Model\ChannelOptions;

$options = new ChannelOptions('http://localhost/hook.php', 'tag');

// Using $client from previous sample

$channel = $client->prepare($options);

object(Gockets\Model\Channel) {
  ["publisherToken":private] => string(32) "f177965656399535ea241a3da40dfcbf"
  ["subscriberToken":private] => string(32) "90b09a2e2d43c83ed907854a46c710fd"
  ["hookUrl":private] => string(25) "http://localhost/hook.php"
  ["tag":private] => string(3) "tag"
  ["listeners":private] => int(0)
}

$publisherToken = '95e9aca9575c29ca8cdc92e54767d783';

$channel = $client->show($publisherToken);

$channels = $client->showAll();

use Gockets\Model\ChannelOptions;

$options = new ChannelOptions('http://localhost/new_hook.php', 'someApplication|tagged');

$updatedChannel = $client->update($channel->getPublisherToken(), $options);

$data = [
    'data' => 'content',
];

$response = $client->publish($data, $channel->getPublisherToken());

object(Gockets\Model\Response) {
  ["success":private] => bool(true)
  ["type":private] => string(3) "INF"
  ["message":private] => string(38) "Successfully pushed data to subscriber"
}

$response = $client->close($channel->getPublisherToken());

echo $response->getMessage(); // Outputs "Successfully closed connection"

use Gockets\Exception\ChannelNotFoundException;

try {
    $client->show('some-publisher-token');
} catch (ChannelNotFoundException $exception) {
    // Your logic when publisher token was not found
}