45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
import Echo from "laravel-echo";
|
|
import Pusher from "pusher-js";
|
|
|
|
window.Pusher = Pusher;
|
|
|
|
export default function initEcho() {
|
|
const echoInstance = new Echo({
|
|
broadcaster: import.meta.env.VITE_BROADCASTER,
|
|
key: import.meta.env.VITE_REVERB_KEY,
|
|
wsHost: import.meta.env.VITE_REVERB_HOST,
|
|
wsPort: Number(import.meta.env.VITE_REVERB_PORT),
|
|
forceTLS: import.meta.env.VITE_FORCE_TLS === 'true',
|
|
disableStats: import.meta.env.VITE_DISABLE_STATS === 'true',
|
|
encrypted: import.meta.env.VITE_ENCRYPTED === 'true',
|
|
cluster: import.meta.env.VITE_CLUSTER,
|
|
enabledTransports: ['ws', 'wss'],
|
|
authorizer: (channel) => {
|
|
return {
|
|
authorize: (socketId, callback) => {
|
|
fetch(`${import.meta.env.VITE_API_URL}/broadcasting/auth`, {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
},
|
|
body: JSON.stringify({
|
|
socket_id: socketId,
|
|
channel_name: channel.name,
|
|
}),
|
|
})
|
|
.then(res => res.json())
|
|
.then(data => callback(null, data))
|
|
.catch(err => callback(err, null));
|
|
},
|
|
};
|
|
},
|
|
});
|
|
|
|
|
|
|
|
window.Echo = echoInstance;
|
|
return echoInstance;
|
|
}
|