Laravel Echo not listening for pusher events

爷,独闯天下 提交于 2021-02-10 20:23:48

问题


Trying to create a kind of chat app with laravel and vuejs. Once the message is sent, I fire the event from laravel which reflects on the pusher debug console with the right event class but the listen callback from vuejs is not called at all.

created () {
        window.Echo.channel('chat')
            .listen('MessageSent', (e) => {
                console.log(e); //not getting this
                this.sentMessages.push({
                    message: e.message.message,
                    user: e.user
                });
            });

    },

Below is a screenshot of the debug console from pusher see the image here as am told I can't embed images yet


回答1:


Try this:

created () {
        window.Echo.channel('chat')
            .listen('App\\Events\\Chats\\MessageSent', (e) => {
                console.log(e);
                this.sentMessages.push({
                    message: e.message.message,
                    user: e.user
                });
            });

    },

By default, Laravel will broadcast the event using the event's class name. However, you may customize the broadcast name by defining a broadcastAs method on the event:

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

The above was copy pasted from Laravel Broadcast Name

My recommendation:

I have always used private channels for chat and you must too. Read here why



来源:https://stackoverflow.com/questions/54223245/laravel-echo-not-listening-for-pusher-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!