如何使用Laravel的Echo绑定“pusher:subscribe”事件

时间:2017-04-28 12:23:05

标签: php laravel laravel-5.4 pusher laravel-echo

在我的laravel应用程序中,我正在使用laravel ECHO。在这里我试图通过 on 方法听一个事件“pusher:subscribe”。但我没有发生。我可以按照laravel echo文档订阅,add_memeber,remove_memeber。但我无法专门听“推送:订阅”事件。实际上我正试图从这个事件中获取数据,我将进一步操纵

window.Echo = new Echo({
                broadcaster: 'pusher',
                key: 'myKeyHere',
                authEndpoint: 'pusher/auth',
                auth: {
                    headers: {

                    },
                    params: {
                        username: this.username
                    }
                }
            });


 window.Echo.join('openChat').here((users) => {
                console.log(users);

            }).listen('MessagePublished', (e) => {
                console.log('Message Received');

            }).on('pusher:subscribe', (users) => {
                console.log('NOT WORKING');

            }).joining((user) => {
                console.log('User Joined');
            }).leaving((user) => {
                console.log('User gone');
            });

1 个答案:

答案 0 :(得分:3)

在对文档和论坛进行深度查找后,我理解pusher:subscribe是一个内部事件,因此无法通过Laravel ECHO捕获。但是我使用pusher:subscription_succeeded来捕获成员信息..

修改后的代码:

window.Echo = new Echo({
            broadcaster: 'pusher',
            key: 'myKeyHere',
            authEndpoint: 'pusher/auth',
            auth: {
                headers: {

                },
                params: {
                    username: this.username
                }
            }
        });


  window.Echo.join('openChat').here((users) => {
            console.log(users);

        }).listen('MessagePublished', (e) => {
            console.log('Message Received');

        }).on('pusher:subscription_succeeded', (member) => {
            console.log(member);

        }).joining((user) => {
            console.log('User Joined');
        }).leaving((user) => {
            console.log('User gone');
        });