question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redis keyPrefix Problem

See original GitHub issue
  • Echo Version: 1.5.3
  • Laravel Version: 5.8.*
  • PHP Version: 7.2.17
  • NPM Version: 6.8.0
  • Node Version: 8.10.8

Description:

When Echo used with Redis, it produces keyPrefix to every channel you created. (which is by default “laravel_database_”)

Steps To Reproduce:

1-) Use Redis as Broadcast Driver and Queue Connection

BROADCAST_DRIVER=redis
QUEUE_CONNECTION=redis

2-) Create any Event

class UserRegistered implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('test');
    }

    public function broadcastAs()
    {
        return 'user.registered';
    }

    public function broadcastWith()
    {
        return ['id' => $this->user->id];
    }
}

3-) Broadcast this event with Private Channel called ‘test’ 4-) Authorize this channel

Broadcast::channel('test', function ($user) {
    return $user;
});

5-) Initialize Echo in your js file

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    authEndpoint: "/broadcasting/auth",
});

6-) Initialize your channel

 Echo.private(`test`)
                .listen('.user.registered', function (e) {
                    console.log(e);
                });

7-) Send event

event(new UserRegistered($user));

😎 See laravel-echo-server logs

[6:13:51 AM] - W3drJJWfqL9OacTmAAAa authenticated for: private-test
[6:13:51 AM] - W3drJJWfqL9OacTmAAAa joined channel: private-test
Channel: foo_bar_database_private-test
Event: user.registered

My Redis keyPrefix is “foo_bar_database_”, so the channel name that the event emitted is “foo_bar_database_private-test” but i am listening the channel “private-test”. So Echo never gets the event.

I tried to delete “private-” prefixes from both php&js side but this time Echo not requiring to authenticate channel anymore since not see as private channel.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
AdSeguracommented, May 27, 2019

I was able to get it working:

In config/database.php disable prefix for redis.

Now i can do

 window.Echo.private('channel')

And in routes/channels.php

Broadcast::channel('channel', function ($user) {

With redis-cli monitor 👍

1558979801.909666 [0 172.20.0.1:35198] "PUBLISH" **"private-channel"** "{\"event\":\"App\\\\Events\\\\PrivateMessage\",\"data\":{\"msg\":\"Hello\",\"user\":\"foo\",\"socket\":\"vDVEap6hCjgJ5OhtAAAB\"},\"socket\":\"vDVEap6hCjgJ5OhtAAAB\"}"

And Echo clients gets their message.

3reactions
omarjebaricommented, Mar 23, 2020

As you’re using Laravel just edit your .env file and set:

REDIS_PREFIX=

This will remove the ‘foo_bar_database_’ prefix from your channel names. Then make sure you’ve restarted your services.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why spring.cache.redis.key-prefix overrides @Cacheable ...
In my application.yml, I defined a prefix for my cache keys: spring: cache: redis: key-prefix: com.example.cache.
Read more >
Redis Clustering Best Practices with Multiple Keys
Learn Redis clustering best practices including the use of Redis multiple keys, Redis cluster transactions, and Redis hashtags.
Read more >
Distributed Output Cache: When using 1 Redis instance for ...
Question/Problem Description. When using Redis as an Output Cache provider, the KeyPrefix is not taken into account.
Read more >
Untitled
Laravel redis key prefix. ... 8 Echo laravel/echo#238 Closed using Redis driver for queue, and problem with notifications. laravel_cache is the default ...
Read more >
Redis 3.2 — Query Keys and Delete Caches by Key Prefix/Pattern ...
In this case, the problem will become: How to get all keys with prefix or pattern; How to clean all caches with specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found