• Stars
    star
    144
  • Rank 247,531 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP library to communicate with Centrifugo HTTP API

phpcent

Build Status Latest Version

PHP library to communicate with Centrifugo v4 HTTP API (for Centrifugo v3 use phpcent v4.x).

Library is published on the Composer: https://packagist.org/packages/centrifugal/phpcent

composer require centrifugal/phpcent:~5.0

See Centrifugo server API documentation.

Basic Usage:

$client = new \phpcent\Client("http://localhost:8000/api");
$client->setApiKey("Centrifugo API key");
$client->publish("channel", ["message" => "Hello World"]);

You can use phpcent to create connection token (JWT):

$token = $client->setSecret("Centrifugo secret key")->generateConnectionToken($userId);

Connection token that will be valid for 5 minutes:

$token = $client->setSecret("Centrifugo secret key")->generateConnectionToken($userId, time() + 5*60);

It's also possible to generate channel subscription token:

$token = $client->setSecret("Centrifugo secret key")->generateSubscriptionToken($userId, $channel);

It's also possible to generate channel subscription token with expiration time, for example token for 30 mins:

$token = $client->setSecret("Centrifugo secret key")->generateSubscriptionToken($userId, $channel, time() + 30*60);

Also API key and secret can be set in constructor:

$client = new \phpcent\Client("http://localhost:8000/api", "<API key>", "<secret key>");

Timeouts:

$client->setConnectTimeoutOption(0); // Seconds | 0 = never
$client->setTimeoutOption(2); // Seconds

All available API methods:

$response = $client->publish($channel, $data);
$response = $client->broadcast($channels, $data);
$response = $client->unsubscribe($channel, $userId);
$response = $client->disconnect($userId);
$response = $client->presence($channel);
$response = $client->presenceStats($channel);
$response = $client->history($channel);
$response = $client->historyRemove($channel);
$response = $client->channels();
$response = $client->info();

To use assoc option while decoding JSON in response:

$client->setUseAssoc(true);

SSL

In case if your Centrifugo server has invalid SSL certificate, you can use:

$client->setSafety(false);

You can also use self signed certificate in safe manner:

$client = new \phpcent\Client("https://localhost:8000/api");
$client->setCert("/path/to/certificate.pem");
$client->setCAPath("/ca/path"); // if you need.

Note: Certificate must match with host name in Client address (localhost in example above).

DNS Resolution

This error may indicate your system is having trouble resolving IPv6 addresses:

cURL error: Resolving timed out after [value] milliseconds

By default, both IPv4 and IPv6 addresses will attempt to be resolved. You can force it to only resolve IPv4 addresses with:

$client->forceIpResolveV4();

Testing

Requirements:

The provided PHPUnit tests assume that a local Centrifugo server is running and available at port 8000. This can be accomplished using Docker and the official Centrifugo image.

# Install package dependencies.
$ composer install

# The following command starts a Centrifugo server running in a background Docker container.
$ docker run -d -p 8000:8000 --name centrifugo centrifugo/centrifugo centrifugo --api_insecure

# Run the test suite.
$ vendor/bin/phpunit

# Shut down the Centrifugo container.
$ docker stop centrifugo

Authors

More Repositories

1

centrifugo

Scalable real-time messaging server in a language-agnostic way. Self-hosted alternative to Pubnub, Pusher, Ably. Set up once and forever.
Go
7,470
star
2

centrifuge

Real-time messaging library for Go with scalability in mind. The core of Centrifugo server.
Go
838
star
3

centrifuge-js

JavaScript client SDK to communicate with Centrifugo and Centrifuge-based server from browser, NodeJS and React Native. Supports WebSocket, HTTP-streaming over Fetch and Readable Stream API, EventSource, WebTransport and SockJS.
TypeScript
353
star
4

centrifuge-go

Go client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
Go
191
star
5

centrifuge-dart

Dart (Flutter) client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
Dart
89
star
6

gocent

Go library to communicate with Centrifugo HTTP API
Go
71
star
7

pycent

Python library to communicate with Centrifugo HTTP API
Python
63
star
8

centrifuge-java

General Java and Android client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
Java
56
star
9

centrifuge-mobile

iOS and Android clients for Centrifugo and Centrifuge library using gomobile on top of centrifuge-go
Go
56
star
10

centrifuge-android

Android client to communicate with Centrifugo v1 over Websockets (not maintained anymore)
Java
47
star
11

examples

Collection of examples for Centrifugo stack https://centrifugal.dev
PHP
46
star
12

centrifuge-swift

Swift client SDK for bidirectional real-time communication with Centrifugo and Centrifuge-based server over WebSocket
Swift
43
star
13

web

Admin web interface for Centrifugo real-time messaging server
TypeScript
35
star
14

adjacent

Centrifugo v1 integration with Django framework, for Centrifugo v2 use cent Python client to integrate
Python
32
star
15

centrifuge-ios

Swift client to communicate with Centrifugo v1 from iOS over WebSocket (not maintained anymore)
Swift
28
star
16

helm-charts

Official Centrifugo Helm chart for Kubernetes
Mustache
27
star
17

centrifuge-python

Centrifugo real-time WebSocket SDK for Python on top of asyncio
Python
26
star
18

rubycent

Ruby gem to communicate with Centrifugo HTTP API
Ruby
19
star
19

documentation

Centrifugo v1 documentation
CSS
14
star
20

jscent

Node.js client to interact with Centrifugo v1 HTTP API
JavaScript
11
star
21

protocol

Centrifuge client-server protocol definitions
Go
7
star
22

rotor

Rotor is a high-performance PUB/SUB Broker and Presence Manager for Centrifuge and Centrifugo based on Tarantool Cartridge (EXPERIMENTAL)
Lua
7
star
23

centrifugal.dev

Documentation site for Centrifugo
JavaScript
7
star
24

centrifugo-pro

A home for Centrifugo PRO releases
3
star
25

tarantool-centrifuge

Base Lua module to build Centrifugo Tarantool Engine. Work in progress.
Lua
3
star
26

homebrew-centrifugo

Homebrew formula for Centrifugo
Ruby
2
star