import Echo from 'laravel-echo'; import Pusher from 'pusher-js'; let echoClient: Echo<'reverb'> | null = null; function environmentString(key: string): string | undefined { const value = import.meta.env[key]; return typeof value === 'string' && value !== '' ? value : undefined; } export function getEcho(): Echo<'reverb'> { if (echoClient !== null) { return echoClient; } const scheme = environmentString('VITE_REVERB_SCHEME') ?? 'http'; const forceTLS = scheme === 'https'; echoClient = new Echo<'reverb'>({ broadcaster: 'reverb', Pusher, key: environmentString('VITE_REVERB_APP_KEY') ?? 'local-key', wsHost: environmentString('VITE_REVERB_HOST') ?? window.location.hostname, wsPort: Number(environmentString('VITE_REVERB_PORT') ?? 8080), wssPort: Number(environmentString('VITE_REVERB_PORT') ?? 443), forceTLS, enabledTransports: ['ws', 'wss'], }); return echoClient; }