centrifugal / phpcent Goto Github PK
View Code? Open in Web Editor NEWPHP library to communicate with Centrifugo HTTP API
License: MIT License
PHP library to communicate with Centrifugo HTTP API
License: MIT License
I'm upgrading my application to Centrifugo 4, and I'm getting a Connection Closed: 3500 invalid token
error when I try to create a new subscription to a channel using a JWT created by generatePrivateChannelToken()
.
The server logs the following:
[INF] token user mismatch channel=$widget:206766-227029_52a5 client=[uuid] tokenUser= user=5555
where user=5555
came from the connection token's sub claim.
If I set "skip_user_check_in_subscription_token": true
in the server config (as described in the v4 migration steps), then it succeeds:
[DBG] client subscribed to channel channel=$widget:206766-227029_52a5 client=[uuid] user=5555
This migration config option will eventually go away, and I don't want to rely on it.
I noticed generatePrivateChannelToken() lacks a way of setting a user ID into the JWT's sub claim (like is done in the generateConnectionToken()
function right above it). If I modify it and inject my user's ID directly into the sub claim:
$payload['sub'] = '5555';
Then everything works without having to set the skip_user_check_in_subscription_token
config option:
[DBG] client subscribed to channel channel=$widget:206766-227029_52a5 client=[uuid] user=5555
Am I missing something, or does generatePrivateChannelToken
need a way to set the sub claim when subscribing to private channels?
Вот то, что изменилось:
github.com/centrifugal/cent/compare/centrifuge-0.8.0
А если словами, то:
— sha256 алгоритм вместо md5 для всех hmac-ов
— вместо project_id теперь project_key
— необязательно указывать /api в адресе подключения — если этого суффикса нет — то клиент автоматически его сам добавляет — это решает некоторые проблемы когда люди указывают в адресе при инициализации не «http://centrifuge.example.com/api», а просто «http://centrifuge.example.com»
I use lumen framework to install this package with 5.1.0
version.
lumen framework is 10.0.
when I try run this code on my local.
$client = new \phpcent\Client(env('CENTRIFUGO_BROADCAST_URL'), env('CENTRIFUGO_API_KEY'), env('CENTRIFUGO_HMAC_SECRET'));
$client->publish("channel-xxxxxx", ["message" => "Hello World"]);
it throw error
[2024-05-07 05:26:47] local.ERROR: json_decode error: Syntax error {"exception":"[object] (Exception(code: 0): json_decode error: Syntax error at /Users/a10116679/Desktop/project/lumenProject/vendor/centrifugal/phpcent/src/Client.php:389) [stacktrace] #0 /Users/a10116679/Desktop/project/lumenProject/vendor/centrifugal/phpcent/src/Client.php(144): phpcent\\Client->send('publish', Array) #1 /Users/a10116679/Desktop/project/lumenProject/app/Http/Controllers/RetailerSmartCartController.php(41): phpcent\\Client->publish('cart_channel_na...', Array) #2 /Users/a10116679/Desktop/project/lumenProject/vendor/illuminate/container/BoundMethod.php(36): App\\Http\\Controllers\\RetailerSmartCartController->adb(Object(App\\Http\\Requests\\Api\\AdbRequest)) #3 /Users/a10116679/Desktop/project/lumenProject/vendor/illuminate/container/Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}() #4 /Users/a10116679/Desktop/project/lumenProject/vendor/illuminate/container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure)) #5 /Users/a10116679/Desktop/project/lumenProject/vendor/illuminate/container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure)) #6 /Users/a10116679/Desktop/project/lumenProject/vendor/illuminate/container/Container.php(662): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL) #7 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(391): Illuminate\\Container\\Container->call(Array, Array) #8 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(356): Laravel\\Lumen\\Application->callControllerCallable(Array, Array) #9 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(331): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\RetailerSmartCartController), 'adb', Array) #10 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(284): Laravel\\Lumen\\Application->callControllerAction(Array) #11 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(264): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array) #12 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(48): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #13 /Users/a10116679/Desktop/project/lumenProject/vendor/stancl/tenancy/src/Middleware/IdentificationMiddleware.php(36): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #14 /Users/a10116679/Desktop/project/lumenProject/vendor/stancl/tenancy/src/Middleware/InitializeTenancyByRequestData.php(46): Stancl\\Tenancy\\Middleware\\IdentificationMiddleware->initializeTenancy(Object(Laravel\\Lumen\\Http\\Request), Object(Closure), '867b8fb34b15403...') #15 /Users/a10116679/Desktop/project/lumenProject/vendor/illuminate/pipeline/Pipeline.php(183): Stancl\\Tenancy\\Middleware\\InitializeTenancyByRequestData->handle(Object(Laravel\\Lumen\\Http\\Request), Object(Closure)) #16 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(30): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #17 /Users/a10116679/Desktop/project/lumenProject/vendor/illuminate/pipeline/Pipeline.php(119): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #18 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(428): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #19 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(263): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure)) #20 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(171): Laravel\\Lumen\\Application->handleFoundRoute(Array) #21 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(431): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #22 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(167): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure)) #23 /Users/a10116679/Desktop/project/lumenProject/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(112): Laravel\\Lumen\\Application->dispatch(NULL) #24 /Users/a10116679/Desktop/project/lumenProject/public/index.php(28): Laravel\\Lumen\\Application->run() #25 {main} "}
Does someone interested in migrating this library to support Centrifugo v2? We need 2 things:
@sl4mmer hello! Could you please make release of v1.0.5? The latest version currently available is 1.0.4 - https://packagist.org/packages/sl4mmer/phpcent
If set this
$client->setSafety(false);
get error
Notice: Accessing static property phpcent\Client::$safety as non static in Client.php on line 39
nuff sad
I was quite surprised when presence_stats
returned me object instead of array, I looked into sources and found:
private function send($method, $params = [])
{
$response = \json_decode($this->request($method, $params));
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \Exception(
'json_decode error: ' . json_last_error_msg()
);
}
return $response;
}
Im wondering who ever uses json_decode
without assoc
option, at least there must be an option for it, but not hardcoded implicit false
.
I'm running into a problem where my application logs this error with phpcent several times each day:
cURL error: Resolving timed out after 2512 milliseconds
I'm using an internal DNS server and the timeout is 2s. We're IPv4 internally and don't use/publish IPv6 records.
Forcing IPv4 resolution with curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
solves the problem, and haven't had a single error in over 20 days, but that capability isn't exposed.
Would it be possible to let this option be set?
Latest PHP has enables by default VERIFYPEER and VERIFYHOST.
Currently Transport
in UNSAFE mode disables only VERIFYPEER keeping VERIFYHOST enabled. https://github.com/centrifugal/phpcent/blob/master/Transport.php#L33
So, unable to use SSL in localhost with self-signed certificate.
It would be great to have ability to configure SSL specific options like CA file and private key file.
Hello,
I hope this message finds you well. I've been exploring your repository with great interest, particularly regarding its capabilities around message publishing and subscription token generation. While reviewing the code, I noticed that there seems to be no mention of token_hmac_secret_key, which, as I understand, is essential for generating JWT tokens for subscription purposes in certain contexts.
Could you kindly clarify whether the tool is designed solely for publishing messages, without the capacity to generate tokens for subscribing? If it indeed lacks this functionality, could you please advise on the recommended approach to implement token generation for subscriptions? Any guidance or suggestions you could provide would be immensely appreciated.
Thank you for your time and for the efforts you've put into developing this tool. I look forward to your response.
Best regards!
Described in docs: https://fzambia.gitbooks.io/centrifugal/content/server/api.html
Old format sending data as form also supported but it's better to move to JSON (in future versions we will probably remove sending data as form)
Hi! Will this library have support Centrifugo v2?
Doesn't work when many requests need to be made
Hi, I got PHP Compile Error on PHP 7.2
'Default value for parameters with a class type hint can only be NULL' in /usr/local/www/h/vendor/sl4mmer/phpcent/Client.php:18
public function __construct(string $url, string $apikey = '', string $secret = '')
There is a deprecation warning for php 8.1 in vendor/centrifugal/phpcent/src/Client.php
on line 403 in "centrifugal/phpcent": "^5.1" version
when calling $client->generateConnectionToken($userId)
function.
Raw php warning message:
DEPRECATED hash_hmac(): Passing null to parameter #3 ($key) of type string is deprecated in vendor/centrifugal/phpcent/src/Client.php on line 403.
With some tests
transport using solution cannot receive data from TLS, which is used by Centrifugo.
There is a possible solution
does it supports centrifugo v4?
php 7.4.9
composer 1.10.17
Debian 10
RAM 4G
did this
composer require centrifugal/phpcent:~3.0
and got this
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 528384 bytes) in phar:///home/admin/.composer/composer/src/Composer/DependencyResolver/Pool.php on line 214
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 528384 bytes) in phar:///home/admin/.composer/composer/src/Composer/DependencyResolver/Pool.php on line 214
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.
I tried to do
COMPOSER_MEMORY_LIMIT=3G composer require centrifugal/phpcent
COMPOSER_MEMORY_LIMIT=-1 composer require centrifugal/phpcent
set memory_limit = 3G in php.ini
set memory_limit = -1 in php.ini
and got
mmap() failed: [12] Cannot allocate memory
Hello
require DRUPAL_ROOT . '/sites/all/libraries/centrifugo/vendor/autoload.php';
$client = new \phpcent\Client('https://****:8000/api');
$client->setApiKey('****');
$channel = 'core';
$data = [
'action' => 'notification',
'data' => [
'type' => 'warning',
'title' => ' title',
'content' => 'content',
'duration' => 10000,
],
];
$client->publish($channel, $data);
works fine (only I do not like the delay of almost a second for the server response)
just after
print_r($client->history($channel));
print_r($client->presence($channel));
for both requests, server returns 108, not available
Token is invalid in centrifugo.
Hi, thanks for this package!
I would be great if you could add support for using psr/http-client instead of hardcoded curl calls for duing the http calls as that would make it a lot easier to write tests of code that depends on this phpcent.
Either that or at least making the request and send methods protected so that it is possible to extend them and provide our own implementation of the http handling.
Regards,
Tarjei
The latest version on packagist is 2.0.0
since actual version is 2.1.0
.
Update, please
Start Centrifugo like this:
- name: Start Centrifugo
run: docker run -d -p 8000:8000 centrifugo/centrifugo:latest centrifugo --api_insecure
And run test suite: https://github.com/centrifugal/phpcent#testing
Hey this library is not working with new centrifugo
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.