Have a look at this page.
https://laravel.com/docs/12.x/broadcasting#client-pusher-channels
You can use pusher-js on client side to connect to socket.io.
const connectToWebsocket = () => {
const websocket = new Pusher(ws_app_key, {
wsHost: ws_url,
wsPort: ws_port,
forceTLS: false,
encrypted: false,
enableStats: false,
enabledTransports: ['ws', 'wss'],
cluster: ws_cluster,
channelAuthorization: {
endpoint: '/api/broadcasting/auth',
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('yggdraToken'),
},
},
activityTimeout: 1000,
pongTimeout: 1000,
logLevel: 'OFF',
});
You need to add these lines to authenticate.
channelAuthorization: {
endpoint: '/api/broadcasting/auth',
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('yggdraToken'),
},
},