Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - How to register custom broadcaster

I want to register a custom broadcaster with the BroadcastManager without having to change the internal framework code...

Now I have to do something like this in the Illuminate\Broadcasting\BroadcasterManager class:

protected function createMyCustomDriver(array $config) {
  // return instance....
}

There is an extend method however, but I don't know if it's ment for this use case or how to use it...

The goal is to use a Broadcaster implementation that uses ZMQ to send these broadcasted events to the WebSocket php server instance.

Any help appreciated!

edit: Link to api doc http://laravel.com/api/5.1/Illuminate/Broadcasting/BroadcastManager.html

like image 900
Johannes Avatar asked Dec 10 '22 20:12

Johannes


1 Answers

You need to extend Illuminate\Broadcasting\BroadcastManager\BroadcastManager using a service provider. This is pretty similar to adding a custom guard but here's a super basic example:

Create a new service provider, I've called mine BroadcastServiceProvider, and add the following to the boot method:

/**
 * Bootstrap the application services.
 *
 * @param BroadcastManager $broadcastManager
 */
public function boot(BroadcastManager $broadcastManager)
{
    $broadcastManager->extend('slack', function (Application $app, array $config) {
        return new Slack;
    });
}

All that does is add your broadcast driver (a class which implements the Illuminate\Contracts\Broadcasting\Broadcaster interface which in my example is Slack) to the broadcast manager with the name slack (you can call your broadcaster anything you like).

Make sure you add this service provider to you app.php config file.

Then, in your broadcasting.php config file add your new driver as a connection. Mine looks something like this:

'connections' => [

    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_KEY'),
        'secret' => env('PUSHER_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
    ],

    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
    ],

    'log' => [
        'driver' => 'log',
    ],

    'slack' => [
        'driver' => 'slack'
    ]

],

You'll notice that the driver name is the same as what's in the service provider extend call. You can call the connection anything you like really and you can add extra parameters which are passed into the service provider should you need them.

After that, your custom broadcaster is registered and ready for use!

like image 178
edcs Avatar answered Dec 24 '22 04:12

edcs