Coder Social home page Coder Social logo

php-centrifugo's Introduction

php-centrifugo - v2, v1

Latest Stable Version Total Downloads License

PHP client for Centrifugo real-time messaging server

Features

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

Quick Examples

Create Centrifugo client

<?php

use Centrifugo\Centrifugo;
use Centrifugo\Clients\HttpClient;

$centrifugo = new Centrifugo('http://example.com/api/', 'secret api key', new HttpClient());

Create Centrifugo client with Redis API support

<?php

use Centrifugo\Centrifugo;
use Centrifugo\Clients\RedisClient;
use Centrifugo\Clients\HttpClient;
use Centrifugo\Clients\Redis\RedisTransport;
use Centrifugo\Clients\Redis\PredisTransport;

// Create Redis transport

$redis = new \Redis();
$redis->connect('localhost');
$redisTransport = new RedisTransport($redis);

// Or Predis transport

$predis = new Predis\Client(['host'   => 'localhost']);
$redisTransport = new PredisTransport($predis);

// Create Centrifugo RedisClient

$centrifugoRedisClient = new RedisClient($redisTransport);
$centrifugoRedisClient->setShardsNumber(12);

// Add Centrifugo HttpClient as failover

$centrifugoHttpClient = new HttpClient();
$centrifugoRedisClient->setFailover($centrifugoHttpClient);

$centrifugo = new Centrifugo('http://example.com/api/', 'secret api key', $centrifugoRedisClient);

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([$channel], $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
}

Related Projects

centrifugo-bundle (under development)

php-centrifugo's People

Contributors

atukai avatar oleh-ozimok avatar oleh-ozimok-quarks-tech avatar vanchelo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

php-centrifugo's Issues

Can I use this library between backend to centrifugo server comunication?

Regardless documentation
https://centrifugal.github.io/centrifugo/server/api/
We need to pass "Authorization: apikey " header for auth via http api, but this library send
request in this method \Centrifugo\Clients\HttpClient::processRequest
and passing olny this headers:

            'Content-Type: application/json',
            'X-API-Sign: ' . $this->generateHashSign(),

How can I pass "Authorization: apikey " or this library did not support server to server comunication via http api?

Could you push a stable version?

Hi - and thanks for writing this library!

Would you mind publishing a proper version so we do not have to hit github for each composer install?

Kind regards,
Tarjei

Redis AUTH

Hi, when using the Redis protocol, you can not use authorization?
Please add the method AUTH through the config:

'redis' => [
   'host'         => 'localhost',
   'port'         => 6379,
   'db'           => 0,
   'shardsNumber' => 0,
   'auth' => 'YOUR_AUTH_TOKEN'
],

Presense request

Hello! How can i send presence request when redis connection is already set?

Semantic versioning

I noticed that the version has been updated to 2.0.0.

According to the semantic versioning scheme, a major update contains backwards incompatible changes. Could you please document these changes (e.g. in an UPGRADE.md file) so that we know what to change/expect when updating to the next major version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.