Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config/database.php redis prefix question #28701

Closed
xiaohuilam opened this issue Jun 2, 2019 · 17 comments
Closed

config/database.php redis prefix question #28701

xiaohuilam opened this issue Jun 2, 2019 · 17 comments
Labels

Comments

@xiaohuilam
Copy link
Contributor

xiaohuilam commented Jun 2, 2019

  • 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

@laurencei
Copy link
Contributor

laurencei commented Jun 2, 2019

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?

@xiaohuilam
Copy link
Contributor Author

xiaohuilam commented Jun 3, 2019

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

@driesvints
Copy link
Member

This is the same issue as laravel/echo#232

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

@driesvints
Copy link
Member

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: tlaverdure/laravel-echo-server#428

@driesvints
Copy link
Member

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

@driesvints
Copy link
Member

I spent quite some time debugging this. The event gets published to Redis here:

$connection->publish($channel, $payload);

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.

@jbrooksuk
Copy link
Member

[..] 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.

@xiaohuilam
Copy link
Contributor Author

[..] 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).

@gimtonic
Copy link

gimtonic commented Nov 2, 2019

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 => " "

@driesvints
Copy link
Member

This is now fixed thanks to @tlaverdure in #30597

@scellige
Copy link

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

@driesvints driesvints reopened this Nov 21, 2019
@tlaverdure
Copy link
Contributor

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

@driesvints
Copy link
Member

@tlaverdure
Copy link
Contributor

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

@scellige
Copy link

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!

@gimtonic
Copy link

Thanks

@flagshippro
Copy link

how can I get key prefix directly from the env file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants