can't remove laravel_database_ prefix from channel
Asked Answered
B

3

5

I'm setting up Laravel echo to broadcast events. But whenever I try to broadcast to a channel the channel name gets an automatic prefix: 'laravel_database_'

I've tried switching the return inside the Event to a regular 'Chanel' as following:

public function broadcastOn()
{
return new Channel('public');
}

but when I look into the laravel-echo-server logs I see it is still being broadcasted on: 'laravel_database_public'.

This way I would need to do the following in my JS:

Echo.channel('laravel_database_public').listen('MessageSent', ({message}) => {
                console.log(message);
            });

But ofcourse, I want to remove the prefix (or figure out why its there). Hopefully someone can clear this up for me. Thanks in advance.

Bartlet answered 5/8, 2019 at 14:3 Comment(0)
D
6

This is configurable in config/database.php (and I believe even removable) under

'redis' => [
    'options' => [
        'prefix' => // change here.
    ]
]
Dunham answered 5/8, 2019 at 14:13 Comment(1)
Thanks alot. Exactly what I was looking for.Bartlet
W
4

The accepted answer does not work with laravel-echo-server.

The solution is rather to let the whole Redis Laravel config untouched and to run version ^1.6.0 of laravel-echo-server with the proper keyPrefix option in laravel-echo-server.json config file:

{
   "databaseConfig": {
     "redis": {
       "keyPrefix": "laravel_database_"
      }
  }
}

Source

Wsan answered 29/11, 2019 at 8:56 Comment(2)
Thanks for this additional answer. I am currently making a new project and will update the answer if what you say is true.Bartlet
It is actually working if you set REDIS_PREFIX="". Regardless, namespacing the Laravel events in the Redis database is certainly a good idea.Wsan
B
1

As at Laravel 7, the config/database.php looks like this

'redis' => [

        'client' => env('REDIS_CLIENT', 'predis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_database_'),
        ],
    ]

So it searches the .env file for your REDIS_PREFIX variable, if there's none, then it generates laravel_database_ or based on whatever you set APP_NAME to in your .env file.

All you have to do is set your REDIS_PREFIX. You can leave it empty so that there is no prefix at all.

Brookner answered 4/5, 2020 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.