New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

zavrik/laravel-centrifugo

1,318
Install
composer require zavrik/laravel-centrifugo
Latest Version:0.3.1
PHP:^8.0
Laravel:9
Older releases:8
License:MIT
Last Updated:Jun 30, 2025
Links: GitHub  ·  Packagist
Maintainer: Zavrik94

Laravel-Centrifugo For Docker

This package is an easier way for use Centrifugo v3 with Laravel Framework and Docker. It realises proxy connections inside docker for trigger events when we have some data in WebSocket Connection.

Install

  1. Copy to .env
CENTRIFUGO_APP_DOCKER_URL=http://<YOUR_CENTRIFUGO_CONTAINER_NAME>:<YOUR_CENTRIFUGO_PORT>
CENTRIFUGO_PROXY_DOCKER_URL=http://<YOUR_CONTAINER_NAME_FOR_PROXY_REDIRECT>
CENTRIFUGO_TOKEN_HMAC_SECRET_KEY=37246e8e-6943-444f-9f5c-c8d3241af361
CENTRIFUGO_ADMIN_PASSWORD=c32c421a-2123-4e8b-b063-c849e6dab599
CENTRIFUGO_ADMIN_SECRET=d37f2351-24e6-4373-9462-c2030e0eaf40
CENTRIFUGO_API_KEY=719521ac-72ce-4621-ad36-5fe0c8cbfd47
  1. execute command php artisan laravel:centrifugo:init and clear cache.
  2. Add to your docker-compose file this container
  centrifugo:
    container_name: centrifugo
    image: centrifugo/centrifugo:v3
    volumes:
      - ./docker/centrifugo/config.json:/centrifugo/config.json
    command: centrifugo -c config.json
    ports:
      - '8000:8000'
    ulimits:
      nofile:
        soft: 65535
        hard: 65535
    networks:
      - <YOUR DOCKER NETWORK>

Usage

Autogenerate config

Command php artisan laravel:centrifugo:init generate config/centrifugo.php for Laravel and docker/centrifugo/config.json for Centrifugo. It Generates for you default proxy routes by .env and centrifugo keys.

Security

By Default, it uses Laravel Passport Token for identify users and possible to connect Centrifugo. You can write your own Guard by implement AuthGuardInterface.

interface AuthGuardInterface
{
    public function auth(Request $request): int;

    public function encodeUserId(int $userId): string;

    public function decodeUserId(string $hashedUserId): int;
}

auth(Request $request): int

Give you Request with body that you can send, and you must return userID by whom WebSocket was connected.

encodeUserId(int $userId): string

Encoding User ID in unique string for security display User ID. By Default in use Hashids library.

decodeUserId(string $hashedUserId): int

Decoding Hashed User ID for correctly setUp User ID in events.

Proxy Events

Centrifugo has 4 proxy routes. We use only 3 of them because of refresh proxy unused by AuthGuard Authentication. You can select which of Users can connect to Centrifugo by Guarding You Application Token.

connect

After Successfully Guarded Your token it dispatches ConnectEvent with UserID.

subscribe

Dispatch SubscribeEvent with UsedID and Channel Name after User successfully subscribed on Channel

publish

Dispatch PublishEvent with UserID, Channel and Published Data after User successfully published data in Channel

FrontEnd usage

You must use default Centrifugo JS library. Example with default BearerToken Connection:

const centrifuge = new Centrifuge("ws://localhost:8000/connection/websocket");
centrifuge.setConnectData({
    token: bearerToken
});
centrifuge.connect();
let subscription  = centrifuge.subscribe(channel, function(ctx) {
    container.innerHTML = ctx.data.value;
    document.title = ctx.data.value;
});

By centrifuge.setConnectData you can send Any data to you Request Body in AuthGuard.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

651,963,193 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982
laravel/tinker

Powerful REPL for the Laravel framework.

518,004,183 7,444
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

435,097,283 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

407,699,310 4,663

Version History

Version Released PHP Laravel License
0.3.1 ^8.0 ^9.0 MIT
0.3 ^8.0 ^8.0.0 MIT
0.2 ^8.0 ^8.0.0 MIT
0.1 ^8.0 ^8.0.0 MIT