maksimusyan/php-centrifugo

Centrifugo 实时消息服务器 PHP 客户端

v1.0.5 2018-03-16 09:19 UTC

This package is not auto-updated.

Last update: 2024-09-19 03:17:35 UTC


README

Centrifugo 实时消息服务器 PHP 客户端

特性

  • 支持通过 Redis 引擎 API 监听器发布消息(仅支持发布、广播、取消订阅、断开连接方法)
  • 支持传输链(Redis + HTTP)作为故障转移。如果 Redis 不可用(或 Redis 传输不支持该方法),客户端尝试通过 HTTP 传输发送消息
  • 支持批量请求

注意!

该库基于 php 扩展 PhpRedis

安装

composer require maksimusyan/php-centrifugo

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

或者直接从 github 的开发者版本安装

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

快速示例

创建 Centrifugo 客户端

<?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,
    ],
]);

向 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
}

发送批量请求

<?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
}