centrifugal/phpcent
PHP client for Centrifugo v5 HTTP API. Publish/broadcast messages, manage subscriptions, presence, history, channels and info, run batch calls, and configure timeouts. Also generates JWT connection and channel subscription tokens using your Centrifugo secret.
Real-time Pub/Sub Alignment: The package is a direct fit for Laravel applications requiring real-time features (e.g., notifications, live updates, collaborative tools) via Centrifugo’s HTTP API. The alignment with Centrifugo’s v5 HTTP API ensures compatibility with modern deployments, while maintaining backward compatibility for existing Laravel integrations.
v6.0.x release’s cleanup and API method extensions (e.g., batch, presenceStats) align with Centrifugo’s latest protocol, reducing edge-case failures.generateConnectionToken).Scalability Considerations:
Stack Compatibility:
phpcent\Client in AppServiceProvider).phpcent v5.x (deprecated in favor of v6.x).phpcent v4.x (unsupported in v6.x).publish() calls can be dispatched to centrifugo-publish queue).Migration Path:
phpcent v6.0.3 + Centrifugo v5.x for latest features (e.g., batch API, improved token auth).phpcent v5.x (no breaking changes).phpcent v4.x (drop PHP <7.0 support).Compatibility Notes:
setCert(), setCAPath()) and IPv6/IPv4 resolution (forceIpResolveV4()).setConnectTimeoutOption() and setTimeoutOption() (critical for unreliable networks).setUseAssoc(true)) for Laravel-friendly responses.Centrifugo Deployment:
phpcent operations.Authentication Flow:
generateConnectionToken) integrate with Laravel’s auth system (e.g., Sanctum, Passport)? Will tokens be stored in the database or issued per-request?Failure Modes:
publish())? The package lacks built-in retries; Laravel’s queue retries or a custom decorator may be needed.Observability:
phpcent doesn’t expose them natively.stderr by default; Laravel’s Monolog integration may be required.Testing:
docker run -p 8000:8000 centrifugo/centrifugo).Alternatives:
ratchet/ratchet)? Evaluate tradeoffs:
Laravel Integration Points:
// app/Providers/CentrifugoServiceProvider.php
public function register()
{
$this->app->singleton(\phpcent\Client::class, function ($app) {
return new \phpcent\Client(
config('centrifugo.api_url'),
config('centrifugo.api_key'),
config('centrifugo.secret_key')
)->setUseAssoc(true);
});
}
// config/centrifugo.php
return [
'api_url' => env('CENTRIFUGO_API_URL', 'http://localhost:8000/api'),
'api_key' => env('CENTRIFUGO_API_KEY'),
'secret_key' => env('CENTRIFUGO_SECRET_KEY'),
'timeout' => 2.0,
];
// app/Listeners/PublishCentrifugoEvent.php
public function handle(Published $event)
{
$client = app(\phpcent\Client::class);
$client->publish('notifications', $event->data);
}
public function handle($request, Closure $next)
{
$token = app(\phpcent\Client::class)
->setSecret(config('centrifugo.secret_key'))
->generateConnectionToken(auth()->id());
$request->headers->set('Centrifugo-Token', $token);
return $next($request);
}
Async Workflow:
publish() calls to a queue (e.g., centrifugo-publish) to decouple Laravel from Centrifugo’s network latency:
// In a Laravel job
public function handle()
{
$client = app(\phpcent\Client::class);
$client->publish('channel', ['data' => $this->data]);
}
Testing Strategy:
phpcent\Client (e.g., using Mockery).generateConnectionToken).publish/subscribe).| Phase | Action Items | Dependencies |
|---|---|---|
| Assessment | 1. Audit existing real-time features (e.g., polling, Server-Sent Events). | Dev team, product roadmap. |
How can I help you explore Laravel packages today?