phplaravelpusherservice-providerbroadcasting

Laravel: dynamic configuration for Pusher


I am trying to make the configuration for Pusher in my Laravel app (SaaS) dynamic. Basically I want to store different Pusher configs for different accounts. And call the corresponding config based on the user.

I have tries to change the config in runtime using config()->set('services.pusher.xxx', 'yyyy'), but this doesn't work at any level of the framework, event in a custom ServiceProvider.

I found Laravel's BroadcastManager and tried to override the createPusherDriver() so that I could create a custom instance of PusherBroadcaster with the user's config, but I am not sure how to do that or where to put it!

What is the best-practice/standard way to do that?


Solution

  • I've been using a setup like this in one of my own projects, to set a custom mail config:

    NOTE: Your mileage may vary due to the order in which service providers are loaded in your app.

    Create a serviceprovider like app\Providers\ConfigProvider.php

    public function boot()
    {
        $this->app->booted(function () {
            $shouldSetCustomConfig = true;
            // Set config values from database.
            if($shouldSetCustomConfig) {
                config([
                    'mail.host' => Config::get('mail.host'),
                    'mail.port' => Config::get('mail.port'),
                ]);
            }
        });
    }