Private channel not working with Laravel echo server

后端 未结 4 1671
猫巷女王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:50

    Try to change this line:

    $this->$receiverId = $receiverId;
    

    To this line:

    $this->receiverId = $receiverId;
    

    In your PrivateMessageEvent __construct()

    Update:

    Try to use use a fixed channel id like this:

    window.Echo.private('private-chat.1')
    

    I suggest you also to use presence channel, are private too but with more features, i.e.:

    Echo.join('private-chat.1')
       .listen('PrivateMessageEvent', (e) => {
        console.log(e);
    });
    

    If you use a dinamic channel number like you use, i.e.:

    window.Echo.private(`private-chat.${receiverId}`)
    

    You have to give receiverId a value in javascript, this declaration is a generic room listener but receiverId should be defined, ${receiverId} is a string interpolation.

    You can define receiverId in the template before inluding app.js, for example, using blade syntax:

    
    

    Another think: I want to be clear that, in all the code above, receiverId represent the id of the chat/room a client want join to not the ID of the receiving user.

提交回复
热议问题