Private channel not working with Laravel echo server

后端 未结 4 1706
猫巷女王i
猫巷女王i 2021-01-07 07:13

I\'m getting this JS error on the console:

app.js:167 Uncaught ReferenceError: receiverId is not defined

Here is my complete code:

<

4条回答
  •  甜味超标
    2021-01-07 07:38

    You can set REDIS_PREFIX to NULL to remove the prefix else if it has a value then you must set keyPrefix in the echo server config.

    If REDIS_PREFIX = NULL then do not add keyPrefix.


    Important Notice

    When using broadcastAs(), the call to listen('') call must start with a DOT

    At this moment the behavior when keyPrefix is used is unknown, if you use the prefix settings, please comment on the outcome of the DOT requirement.

    https://laravel.com/docs/6.x/broadcasting#broadcast-name

    public function broadcastAs()
    {
        return 'server.created';
    }
    
    .listen('.server.created', function (e) {
        ....
    });
    

    I would check the DOT + PREFIX combo my self but I feel Laravel Echo is going to give me a heart attack if I work on it any longer.


    If you do not use broadcastAs() then the naming will fallback to the event class name, in this case there is no DOT prefix injected, see the setup below:

    laravel-echo-server.json

    {
        "host": "127.0.0.1",
        "port": "6001",
        "protocol": "http",
    
        "database": "redis",
    
        "databaseConfig": {
            "redis": {
                "host": "127.0.0.1",
                "port": 6379,
                "db": 0,
                "keyPrefix": "VALUE OF REDIS_PREFIX"
            }
    }
    

    /app/Events/MyExample.php

    id = $id;
        $this->payload = $payload;
      }
    
      public function broadcastOn()
      {
        return new PrivateChannel('example.' . $this->id);
      }
    
    }
    
    

    Trigger an event (PHP)

    
    use App\Events\MyExample
    
    $payload = [
     'duck' => 'sauce',
     'Bass' => 'Epic'
    ];
    
    event(new MyExample($id, $payload))
    
    

    Listening for event (JavaScript)

    Echo.private(`example.${id}`).listen('MyExample', event => {
    
      console.log('payload', event.payload)
    
    })
    

提交回复
热议问题