Framework: Why laravel broadcast channel has a prefix?

Created on 14 Apr 2019  ·  20Comments  ·  Source: laravel/framework

  • Laravel Version: 5.8
  • PHP Version: 7.2
  • Database Driver & Version: Mysql/5.7

Description:

Okay, there were a lot of steps, but by taking the test I do not get any feedback from my listener. I noticed that in the laravel-echo-server console, it returns the channel as laravel_database_test-asd.

So I changed the listener channel in my component from channel ('test-asd') to channel ('laravel_database_test-asd') and it works normally.

So my question is: Why is laravel creating a channel with the prefix laravel_database_?

Steps To Reproduce:

  1. I created a new project with laravel 5.8;
  2. I have modified the values ​​of the BROADCAST_DRIVER andQUEUE_DRIVER entries in my .env to redis;
  3. I created a new channel route in the routes/channel.php file with the name test-asd;
  4. I created a new event with the name MessageSent;
  5. In the broadcastOn method I added: `return new Channel ('test-asd');
  6. I created a test route in the routes/web.php file with the name test, and added a callback: broadcast(new MessageSent ());
  7. I installed laravel-echo-server globally;
  8. I installed the depencences with npm install;
  9. I installed laravel-echo with npm install laravel-echo
  10. I installed predis/predis with composer require predis/predis
  11. I modified the resources /js/bootstrap.js file by importing socket.io-client and pointing socket.io as broadcaster;
  12. I've added a listener to the Echo in my TestComponent.vue: Echo.channel('test-asd').listen ('MessageSent', (e) => {};
  13. I started the redis-server, the laravel-echo-server, the artisan serve and the queue:listen;
  14. testing...

Most helpful comment

I found the reason for this issue. Now config/database.php configuration file has several information for connection to the Redis, including a key to set the prefix.

I found a call to this setting in the register method of the Iluminate\Redis\RedisServiceProvider class.

Why is this not documented?

All 20 comments

I found the reason for this issue. Now config/database.php configuration file has several information for connection to the Redis, including a key to set the prefix.

I found a call to this setting in the register method of the Iluminate\Redis\RedisServiceProvider class.

Why is this not documented?

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

Thanks!

@adhenrique

So every channel has prefix xxx_database_

How can I subscribe private channel as the following?

Echo.private(order.${orderId})

@xralphack It's not a perfect solution but you could comment the "prefix" line in database.php. Without that prefix you could then use Echo.private(order.${orderId}) as it is. But as I said it's not a perfect way to go. Best way would just to prefix the channel name like <APP_NAME>_database_<channel-name>

My questions is when client want to join a private channel

echo.private(App.User.${user.id}).

I have no idea how can I add the prefix.

I did not see Echo has any configuration about this.

First of all this is not the right place for such a question. You can get all the information from the Laravel documentation or you could ask this in laravel forms.

But in short: In case you are looking into the default setting of notifications. Check "Customizing The Notification Channel" part under the Laravel notifications section.

If not, you are looking for broadcasting events. Then you should check your event class which you are broadcasting. You can modify the channel name within the broadcastOn method. Don't forget to change it within routes/channels.php as well. For more on this check Laravel broadcast.

Please sentence the laravel-echo-server to death.
Reason is just the procrastination with each laravel version upgrading.

Please sentence the laravel-echo-server to death.
Reason is just the procrastination with each laravel version upgrading.

It's a good library though, I'd say we need it. Has anyone figured how to add the prefix to laravel echo?

I found the reason for this issue. Now config/database.php configuration file has several information for connection to the Redis, including a key to set the prefix.

I found a call to this setting in the register method of the Iluminate\Redis\RedisServiceProvider class.

Why is this not documented?

Hey thank you so much for this. This was driving me insane. I was wondering why using laradock's built-in laravel-echo-server, I was able to join channels but couldn't get to execute the listen callback function of Echo in my JavaScript code. I noticed in the logs that on connecting to the channel, the output joined channel: xyz was different from Channel: MyAppName_database_xyz which led me here.

In the end I just commented out the prefix in config/database.php, I'm not sure what the overall impact is to the application, but for now it works:

    'redis' => [

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

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

This is not documented at all in the current documentation. I'm wondering if this was intentional and I'm missing something or just an oversight.

My findings were that in order to "authenticate" to private channels you cant include that APP_database_private_XXX prefix, but in order to consume you must!.... Laravel echo lib doesn't support it out of the box, so commenting out the 'prefix' is the only way to get this working

Echo.channel('public-event') // doesnt work because is missing the redis prefix
    .listen('.test-event', () => {
        console.log('Public')
    })

Echo.channel('APP_database_public-event') // this works
    .listen('.test-event', () => {
        console.log('Public')
    })

Echo.private('user.1') // this authenticates successfully, but the listener is never call
    .listen('.test-event', () => {
        console.log('Private')
    })

Echo.private('APP_database_private_user.1') // this fails to authenticate
    .listen('.test-event', () => {
        console.log('Private')
    })

I think this issue shouldn't be closed until it gets documented, we've been trying to solve this for 2 days. Thank you @sevillaarvin

I found the reason for this issue. Now config/database.php configuration file has several information for connection to the Redis, including a key to set the prefix.

I found a call to this setting in the register method of the Iluminate\Redis\RedisServiceProvider class.

Why is this not documented?

Thank's, really helped

@milewski
Nice. it is clear. Follow to him.

I found the reason for this issue. Now config/database.php configuration file has several information for connection to the Redis, including a key to set the prefix.
I found a call to this setting in the register method of the Iluminate\Redis\RedisServiceProvider class.
Why is this not documented?

Hey thank you so much for this. This was driving me insane. I was wondering why using laradock's built-in laravel-echo-server, I was able to join channels but couldn't get to execute the listen callback function of Echo in my JavaScript code. I noticed in the logs that on connecting to the channel, the output joined channel: xyz was different from Channel: MyAppName_database_xyz which led me here.

In the end I just commented out the prefix in config/database.php, I'm not sure what the overall impact is to the application, but for now it works:

    'redis' => [

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

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

This is not documented at all in the current documentation. I'm wondering if this was intentional and I'm missing something or just an oversight.

It works !! Thank you

Don t work comment 'prefix' now

Don t work comment 'prefix' now

Is your config being cached? try php artisan config:clear

Dont work

If you're using laravel-echo-server you can specify the prefix in the redis database config:

{
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "keyPrefix": "app_database_"
        },
    }
}

https://github.com/tlaverdure/laravel-echo-server#redis-1

I found the reason for this issue. Now config/database.php configuration file has several information for connection to the Redis, including a key to set the prefix.
I found a call to this setting in the register method of the Iluminate\Redis\RedisServiceProvider class.
Why is this not documented?

Hey thank you so much for this. This was driving me insane. I was wondering why using laradock's built-in laravel-echo-server, I was able to join channels but couldn't get to execute the listen callback function of Echo in my JavaScript code. I noticed in the logs that on connecting to the channel, the output joined channel: xyz was different from Channel: MyAppName_database_xyz which led me here.

In the end I just commented out the prefix in config/database.php, I'm not sure what the overall impact is to the application, but for now it works:

    'redis' => [

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

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

This is not documented at all in the current documentation. I'm wondering if this was intentional and I'm missing something or just an oversight.

Guys it's better to just update your .env file. e.g:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_PREFIX=YOUR_PREFIX_HERE_OR_LEAVE_EMPTY
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shopblocks picture shopblocks  ·  3Comments

gabriellimo picture gabriellimo  ·  3Comments

PhiloNL picture PhiloNL  ·  3Comments

RomainSauvaire picture RomainSauvaire  ·  3Comments

jackmu95 picture jackmu95  ·  3Comments