Code Monkey home page Code Monkey logo

phpcent's Introduction

phpcent

Build Status Latest Version

PHP library to communicate with Centrifugo v2 HTTP API.

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

composer require centrifugal/phpcent:~3.0

See Centrifugo 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 private channel subscription token:

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

Also API key and secret can be set in constructor:

$client = new \phpcent\Client("http://localhost:8000/api", "Centrifugo API key", "Centrifugo 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

phpcent's People

Contributors

fzambia avatar sl4mmer avatar weijer avatar beporter avatar vovan-ve avatar jumper572 avatar khvalov avatar aleksraiden avatar darkkin avatar xaockd avatar juliustm avatar stevetherrien avatar tomchanskiy avatar yagobski avatar

Watchers

James Cloos avatar

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.