gockets-project / gockets-php
PHP client for Gockets https://github.com/gockets-project/gockets
Requires
- php: >=7.2
- ext-json: *
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- phpunit/phpunit: ^8.1
This package is auto-updated.
Last update: 2024-10-19 23:12:51 UTC
README
Gockets is daemon written in Golang to give languages, like PHP a middleware for REST-oriented communication with Websockets.
Gockets PHP
This library provides implemented and ready to use interface for gockets daemon.
Installation via Composer
composer require gockets-project/gockets-php
Quickstart
Setup Gockets server
Setup PHP client
use Gockets\Client; use Gockets\Model\Params; $host = 'localhost'; // Default value $port = '8844'; // Default value $client = new Client(new Params($host, $port));
Prepare channel
Creates new channel. Can accept optional argument instance of Gockets\Model\ChannelOptions
.
use Gockets\Model\ChannelOptions; $options = new ChannelOptions('http://localhost/hook.php', 'tag'); // Using $client from previous sample $channel = $client->prepare($options);
Gockets\Model\Сhannel
example:
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) }
Show channel
Returns specific channel.
$publisherToken = '95e9aca9575c29ca8cdc92e54767d783'; $channel = $client->show($publisherToken);
Show all channels
Returns empty or filled with Gockets\Model\Сhannel
objects array.
$channels = $client->showAll();
Edit channel
Use to modify specific channel attributes (change hook url or tag).
use Gockets\Model\ChannelOptions; $options = new ChannelOptions('http://localhost/new_hook.php', 'someApplication|tagged'); $updatedChannel = $client->update($channel->getPublisherToken(), $options);
Publish data
Send some data to channel.
In this example $channel
variable contains Gockets\Model\Сhannel
object.
$data = [ 'data' => 'content', ]; $response = $client->publish($data, $channel->getPublisherToken());
Gockets\Model\Response $response
example:
object(Gockets\Model\Response) { ["success":private] => bool(true) ["type":private] => string(3) "INF" ["message":private] => string(38) "Successfully pushed data to subscriber" }
Always try to ensure that $success
property of response is true
.
Close channel
Closes connection and removes channel.
$response = $client->close($channel->getPublisherToken()); echo $response->getMessage(); // Outputs "Successfully closed connection"
Error handling
Mostly error handling currently in development, but in case if publisher token was not found library throws Gockets\Exception\ChannelNotFoundException
.
use Gockets\Exception\ChannelNotFoundException; try { $client->show('some-publisher-token'); } catch (ChannelNotFoundException $exception) { // Your logic when publisher token was not found }
In bin
directory located Gockets builded instance for Linux. For more information about Golang project refer to it's page.