Laravel (Echo, Sanctum, websockets) + Pusher + Nuxtjs SPA

你说的曾经没有我的故事 提交于 2021-01-29 07:33:37

问题


I'm trying to send events to private channel, but I can't receive them. Otherwise it works fine for public channels.

Here's my code :

Plugin : Echo.js

window.Echo = new Echo({
    broadcaster: 'pusher',        
    key: process.env.MIX_PUSHER_PUBLIC_KEY,
    wsHost: process.env.VUE_APP_WEBSOCKETS_SERVER , //window.location.hostname,//
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsPort: 6001,
    forceTLS: false,
    /* key: process.env.MIX_ABLY_PUBLIC_KEY,
    wsHost: 'realtime-pusher.ably.io',
    wsPort: 443, */
    disableStats: true,
    encrypted: true,    
    auth: {
        headers: {
            'X-CSRF-TOKEN': Cookies.get('XSRF-TOKEN'),            
        },
    },

    authorizer: (channel, options) => {
        return {
            authorize: (socketId, callback) => {
                axios.post(process.env.VUE_APP_WEBSOCKETS_SERVER+'/api/broadcasting/auth', {
                    socket_id: socketId,
                    channel_name: channel.name
                })
                    .then(response => {
                        callback(false, response.data);
                    })
                    .catch(error => {
                        callback(true, error);
                    });
            }
        };
    },
})

Laravel Broadcasting.php :

'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'useTLS' => false,                
                'encrypted' => true,
                'host' => '127.0.0.1',
                'port' => 6001,
                'scheme' => 'http',
            ],

Api Routes :

Broadcast::routes(['middleware' => ['auth:sanctum']]);

Client Side :

 created() {                                
        Pusher.logToConsole = true;
        Echo.logToConsole = true;           
                
        window.Echo.private('shop')
            .listen('MessageSent', (e) => {
                console.log('Hi' + e)
        }) 
    },

I notice that pusher subscribed to the channel with no errors, but the result in console is like this :

Pusher :  : ["Event sent",{"event":"pusher:subscribe","data":{"channel":"private-shop"}}]

回答1:


Hi maybe this could help you. Please make sure that the response in the authorize function is used correctly. I realize after a while that the response token was send as response not response.data. Try this.

window.Echo = new Echo({
    broadcaster: 'pusher',        
    key: process.env.MIX_PUSHER_PUBLIC_KEY,
    wsHost: process.env.VUE_APP_WEBSOCKETS_SERVER , //window.location.hostname,//
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsPort: 6001,
    forceTLS: false,
    /* key: process.env.MIX_ABLY_PUBLIC_KEY,
    wsHost: 'realtime-pusher.ably.io',
    wsPort: 443, */
    disableStats: true,
    encrypted: true,    
    auth: {
        headers: {
            'X-CSRF-TOKEN': Cookies.get('XSRF-TOKEN'),            
        },
    },

    authorizer: (channel, options) => {
        return {
            authorize: (socketId, callback) => {
                axios.post(process.env.VUE_APP_WEBSOCKETS_SERVER+'/api/broadcasting/auth', {
                    socket_id: socketId,
                    channel_name: channel.name
                })
                    .then(response => {
                        callback(false, response);
                    })
                    .catch(error => {
                        callback(true, error);
                    });
            }
        };
    },
})


来源:https://stackoverflow.com/questions/65856972/laravel-echo-sanctum-websockets-pusher-nuxtjs-spa

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