skeith22
2 months ago

Broadcasting using Redis Driver has a channel naming issue since v5.8

Posted 2 months ago by skeith22

For those who are using Redis as their broadcasting driver, you will have an issue trying to receive the broadcasted events if you are using the channel name normally on your Echo channel listeners.

Since Laravel 5.8 there were changes on the config/database.php on Redis configuration that was not written on the website documentation regarding broadcasting events using Redis.

IF you don't fully specify the channel name including the database prefix you won't receive the broadcasted events.

this is what you should do for those who are having issues.

config/database.php file

// Take note of the prefix
'options' => [
    'cluster' => env('REDIS_CLUSTER', 'predis'),
    'prefix' => Str::slug(env('APP_NAME', 'laravel'), '_').'_database_',
],

so if you're APP_NAME is example "DOMAIN"

APP_NAME=DOMAIN

then your event file example TestEvent

/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn()
{
    return new Channel('test-channel');
}

This is the log on the Laravel Echo Server when you fire the event TestEvent

Channel: DOMAIN_database_test-channel
Event: App\Events\TestEvent

then on your application js file.

Echo.channel('DOMAIN_database_test-channel').listen('TestEvent', (event) => {
    console.log(event);
});

It looks clunky and dirty though but you should be able to receive the broadcasted event.

They should fix the Laravel Echo package to automatically remove the "DOMAIN_database_" prefix so that you can use the channel name normally like

Echo.channel('test-channel').listen('TestEvent', (event) => {
    console.log(event);
});

Is it possible to intercept the Event on the Echo before it passes it on the channel function?

@TaylorOtwell @JeffreyWay

Please sign in or create an account to participate in this conversation.