Framework: config/database.php redis prefix question

Created on 2 Jun 2019  路  17Comments  路  Source: laravel/framework

  • Laravel Version: 5.8.x
  • PHP Version: 7.2

Description:

https://github.com/laravel/laravel/blob/da83285/config/database.php#L126 this config/database.php Redis prefix line, is a hard-code un-customizable prefix (not in .env at least), to set a for all Redis connections.

But the heavy serious question is, it will add prefix named laravel_database_ before your channel name when you trigger an web socket event from laravel application, into laravel_database_private.channel.xxx.

11559478832_ pic_hd


Reference

bug

Most helpful comment

This solved the problem (https://github.com/tlaverdure/laravel-echo-server/releases/tag/1.6.0):

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

@tlaverdure and @driesvints Thank you very much!

All 17 comments

this config/database.php redis prefix line, is a hardcode logic to set a un-customizable prefix for all redis connections.

You can change that prefix to be anything you want. You control that code.

I think the apparent bug is that, if a prefix is set, it is not appended to the channel name? Is that a bug in Laravel Framework, or a bug in Laravel Echo?

this config/database.php redis prefix line, is a hardcode logic to set a un-customizable prefix for all redis connections.

You can change that prefix to be anything you want. You control that code.

I think the apparent bug is that, if a prefix is set, it is not appended to the channel name? Is that a bug in Laravel Framework, or a bug in Laravel Echo?

Yep, laravel-echo client does not support to set a string prefix for channel name.

// the private method will add, and only add `private-` prefix,
//   which mismatch with laravel 5.8 redis driver provides `laravel_database_private-`.
echo.private(`chat.${room_id}`).listen()

I suggest to remove config/database.php's redis.options.prefix option, because in the mix multiple laravel redis structure, better to set different redis database name REDIS_DB / REDIS_CACHE_DB for every app

This is the same issue as https://github.com/laravel/echo/issues/232

I'd say this is a framework issue so we should try to see to strip the prefix before emiting back to echo.

I was trying to debug this but I'm currently stuck with setting up laravel-echo-server. If anyone could help out so I can debug this issue further, that'd be nice: https://github.com/tlaverdure/laravel-echo-server/issues/428

Managed to get it to work. @xiaohuilam I managed to reproduce the issue.

I spent quite some time debugging this. The event gets published to Redis here: https://github.com/laravel/framework/blob/24cc1786bd55876fa52380306354772355345efd/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php#L102

Until this point everything's okay. But the channel probably gets its prefix set as soon as its published on this line. I can't figure out where the event gets pulled out of Redis. I suspect somewhere in Socket.io but I can't find it. And thus I can't figure out a way to strip the prefix again.

If anyone is willing to put in work into this that'd be great.

[..] is a hard-code un-customizable prefix (not in .env at least), to set a for all Redis connections.

This is now customisable out of the box with the REDIS_PREFIX variable.

[..] is a hard-code un-customizable prefix (not in .env at least), to set a for all Redis connections.

This is now customisable out of the box with the REDIS_PREFIX variable.

Better to default empty in .env.example (Existing project won't override the .env by copy .env.example).

Dont work REDIS_PREFIX="" in .env for fix prefix

database.php

'redis' => [

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

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

    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],

    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_CACHE_DB', 1),
    ],

],

Dont work private channels then comment 'prefix' or 'prefix' => "" or 'prefix => " "

This is now fixed thanks to @tlaverdure in https://github.com/laravel/framework/pull/30597

This is now fixed thanks to @tlaverdure in #30597

Unfortunately, it didn't fix. Authorization in private channels works fine, but messages still sending in the wrong channel, using the prefix in the channel name.

prefix

Have you upgraded to the latest version of Laravel Echo Server (1.6.0)?

@tlaverdure there's no such release? https://github.com/tlaverdure/laravel-echo-server/releases

Oops, published to npm but forgot to update the repo.

This solved the problem (https://github.com/tlaverdure/laravel-echo-server/releases/tag/1.6.0):

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

@tlaverdure and @driesvints Thank you very much!

Thanks

how can I get key prefix directly from the env file

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixsanz picture felixsanz  路  3Comments

JamborJan picture JamborJan  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

kerbylav picture kerbylav  路  3Comments