简体   繁体   中英

Change broadcast driver on the fly on laravel

I have multiple pusher accounts for my multi tenant laravel app with this configuration:

'pusher_it' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_IT_KEY'),
        'secret' => env('PUSHER_APP_IT_SECRET'),
        'app_id' => env('PUSHER_APP_IT_ID'),
        'options' => [
            'cluster' => 'eu',
            'useTLS' => true
        ],
    ],

    'pusher_es' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_ES_KEY'),
        'secret' => env('PUSHER_ES_ES_SECRET'),
        'app_id' => env('PUSHER_ES_ES_ID'),
        'options' => [
            'cluster' => 'eu',
            'useTLS' => true
        ],
    ],

    'pusher_pt' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_PT_KEY'),
        'secret' => env('PUSHER_APP_PT_SECRET'),
        'app_id' => env('PUSHER_APP_PT_ID'),
        'options' => [
            'cluster' => 'eu',
            'useTLS' => true
        ],
    ],

When I send a new notification:

Notification::send($usersToNotify, (new VehicleFailureEstimateNotification($vehicleFailureEstimate))->locale(App::getLocale()));

I would set on the fly the pusher configuration:

class VehicleFailureEstimateNotification extends Notification
{
use Queueable;
/**
 * @var VehicleFailureEstimate
 */
private $vehicleFailureEstimate;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct(VehicleFailureEstimate $vehicleFailureEstimate)
{
    Broadcast::setDefaultDriver('pusher_'.$this->locale);
    Broadcast::connection('pusher_'.$this->locale);
    $this->vehicleFailureEstimate = $vehicleFailureEstimate;
}

But the notification is fired always through .env default BROADCAST_DRIVER=pusher_it

Can you help me? Thank you

It has been a long time question but I hope it will help someone. It can be set config in your constructor in your case OR in BroadcastServiceProvider.php in boot method

config()->set('broadcasting.default', 'pusher_'.$this->locale);

I tested it and it works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM