Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Configure Lumen To Broadcast Events via Pusher?

Lumen's docs state that "Lumen supports several broadcast drivers out of the box: Pusher, Redis, and a log driver for local development and debugging. A configuration example is included for each of these drivers. The BROADCAST_DRIVER configuration option may be used to set the default driver."

In my .env file I have set BROADCAST_DRIVER=pusher. Where/how can I configure my pusher ID, key, and secret? I see that in Laravel a configuration file for setting these options lies within config/broadcasting.php. Where can I set these options within Lumen?

Temporarily I have edited Illuminate\Broadcasting\BroadcastManager and hard coded my values in.

protected function createPusherDriver(array $config)
    {
        // override
        $app_id = 'hidden';
        $key = 'hidden';
        $secret = 'hidden';

        return new PusherBroadcaster(
            new Pusher($key, $secret, $app_id, Arr::get($config, 'options', []))
        );
    }
like image 621
Feek Avatar asked Dec 20 '15 04:12

Feek


2 Answers

Okay, I figured it out. Essentially you have to add in the config files yourself.

  1. Create a config directory in the root of your app.
  2. Copy config/broadcasting.php from a working laravel installation into this directory
  3. Add the following keys to your .env file: PUSHER_SECRET, PUSHER_KEY, PUSHER_APP_ID
like image 68
Feek Avatar answered Nov 13 '22 06:11

Feek


In general, Lumen supports two modes of configuration:

  • Setting environment variables consumed by Lumen's internal config files
  • Creating project config files that override Lumen's internal configuration

Lumen provides environment-based configuration variables needed to configure most of the framework's components, including Pusher. Though unclear from the docs, we can also configure Lumen through config files like Laravel. This enables advanced configuration that Lumen may not support through its built-in configuration structure.

By default, new Lumen projects don't provide configuration files like new Laravel projects do in the config/ directory. As @Feek discovered, we can create the config/ directory and add any needed config files. For example, we can create the config/broadcasting.php file to set up broadcast connections.

When creating a config file in the project like this, Lumen will automatically read configuration values from the file if it matches the name of one of the Lumen built-in config files. If we want to add a custom configuration file that doesn't match one of Lumen's internal config filenames, we need to manually instruct Lumen to read the config file in a service provider or in bootstrap/app.php.

For instance, to load configuration values from config/my-custom-config.php, add this line to the application's boot process:

$app->configure('my-custom-config');

In the particular case of this question, Lumen's built-in broadcasting.php config file reads Pusher environment variables for us, so we don't need to create a config file in the project for these. Simply set the BROADCAST_DRIVER, PUSHER_SECRET, PUSHER_KEY, and PUSHER_APP_ID in .env.

like image 41
Cy Rossignol Avatar answered Nov 13 '22 05:11

Cy Rossignol