2017 © Pedro Peláez
 

library php-centrifugo

PHP client for Centrifugo real-time messaging server

image

maksimusyan/php-centrifugo

PHP client for Centrifugo real-time messaging server

  • Friday, March 16, 2018
  • by maksimusyan
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

php-centrifugo - version 1

PHP client for Centrifugo real-time messaging server, (*1)

Features

  • Support publishing messages via Redis engine API listener (publish, broadcast, unsubscribe, disconnect methods only)
  • Support transport chain (Redis + HTTP) as failover. If Redis down (or method not supported by Redis transport) client try send message via HTTP transport
  • Support batch requests

Attention!

The library is based on the php extension PhpRedis, (*2)

Install

composer require maksimusyan/php-centrifugo, (*3)

{
  "require": {
    "maksimusyan/php-centrifugo": "1.0.5"
  }
}

or the developer version directly from github:, (*4)

{
  "repositories": [
    {
      "type": "vcs",
      "url": "git://github.com/maksimusyan/php-centrifugo.git"
    }
  ],
  "require": {
    "maksimusyan/php-centrifugo": "dev-master"
  }
}

Quick Examples

Create Centrifugo client

<?php

use Centrifugo\Centrifugo;

$endpoint = 'http://example.com/api/';
$secret = 'SECRET_API_KEY';

// From Redis transport
$centrifugo = new Centrifugo($endpoint, $secret, [
    'redis' => [
        'host'         => 'localhost',
        // additional params
        'port'         => 6379,
        'db'           => 0,
        'timeout'      => 0.0,
        'shardsNumber' => 0,
        'auth' => 'YOUR_REDIS_AUTH_TOKEN',
    ]
]);

// OR from CURL transport
$centrifugo = new Centrifugo($endpoint, $secret, [
    'http' => [
        // Curl options
        CURLOPT_TIMEOUT => 5,
    ],
]);

Send request to Centrifugo

<?php

use Centrifugo\Centrifugo;
use Centrifugo\Exceptions\CentrifugoException;

$userId = 1;
$channel = '#chan_1';
$messageData = ['message' => 'Hello, world!'];

try {
    //Send message into channel.
    $response = $centrifugo->publish($channel, $messageData);

    //Very similar to publish but allows to send the same data into many channels.
    $response = $centrifugo->broadcast($channels, $messageData);

    //Unsubscribe user from channel.
    $response = $centrifugo->unsubscribe($channel, $userId);

    //Disconnect user by user ID.
    $response = $centrifugo->disconnect($userId);

    //Get channel presence information (all clients currently subscribed on this channel).
    $response = $centrifugo->presence($channel);

    //Get channel history information (list of last messages sent into channel).
    $response = $centrifugo->history($channel);

    //Get channels information (list of currently active channels).
    $response = $centrifugo->channels();

    //Get stats information about running server nodes.
    $response = $centrifugo->stats();

    //Get information about single Centrifugo node.
    $response = $centrifugo->node('http://node1.example.com/api/');
} catch (CentrifugoException $e) {
    // invalid response
}

Send batch request

<?php

use Centrifugo\Centrifugo;
use Centrifugo\Exceptions\CentrifugoException;

$userId = '1'; //must be a string
$channel = '#chan_1';
$messageData = ['message' => 'Hello, world!'];

try {
    $requests[] = $centrifugo->request('publish', ['channel' => $channel, 'data' => $messageData]);
    $requests[] = $centrifugo->request('broadcast', ['channel' => $channel, 'data' => $messageData]);
    $requests[] = $centrifugo->request('unsubscribe', ['channel' => $channel, 'user' => $userId]);
    $requests[] = $centrifugo->request('disconnect', ['user' => $userId]);

    $batchResponse = $centrifugo->sendBatchRequest($requests);

    foreach ($batchResponse as $response) {
        if ($response->isError()) {
            // get error info
            $error = $response->getError();
        } else {
            // get response data as array
            $responseData = $response->getDecodedBody();
        }
    }
} catch (CentrifugoException $e) {
    // invalid response
}

The Versions

16/03 2018

dev-master

9999999-dev https://github.com/maksimusyan/php-centrifugo

PHP client for Centrifugo real-time messaging server

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-curl *

 

real-time centrifugo

16/03 2018

v1.0.5

1.0.5.0 https://github.com/maksimusyan/php-centrifugo

PHP client for Centrifugo real-time messaging server

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-curl *

 

real-time centrifugo

16/03 2018

v1.0.4

1.0.4.0 https://github.com/maksimusyan/php-centrifugo

PHP client for Centrifugo real-time messaging server

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-curl *

 

real-time centrifugo

07/06 2017

v1.0.x-dev

1.0.9999999.9999999-dev https://github.com/oleh-ozimok/php-centrifugo

PHP client for Centrifugo real-time messaging server

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Oleh Ozimok

real-time centrifugo

07/06 2017

v1.0.3

1.0.3.0 https://github.com/oleh-ozimok/php-centrifugo

PHP client for Centrifugo real-time messaging server

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Oleh Ozimok

real-time centrifugo

22/03 2017

dev-develop

dev-develop https://github.com/oleh-ozimok/php-centrifugo

PHP client for Centrifugo real-time messaging server

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-curl *

 

by Oleh Ozimok

centrifugo