2

I am trying to install and use laravel-echo and pusher-js. I have the following:

bootstrap.ts

import Pusher from 'pusher-js';
import Echo from 'laravel-echo';

window.Pusher = Pusher;

window.Echo = new Echo({
  broadcaster: 'pusher',
  key: import.meta.env.VITE_PUSHER_APP_KEY,
  wsHost: import.meta.env.VITE_PUSHER_HOST ?? `ws-${import.meta.env.VITE_PUSHER_CLUSTER}.pusher.com`,
  wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
  wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
  forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
  enabledTransports: ['ws', 'wss'],
});

However, I get two errors

Property 'Pusher' does not exist on type 'Window & typeof globalThis'.
Property 'Echo' does not exist on type 'Window & typeof globalThis'.

What is going wrong here?

2

3 Answers 3

2

I have the same problem while learning laravel with react typescript, this one worked for me

import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;

//add these
declare global {
    interface Window {
        Pusher: any; 
        Echo: Echo; 
    }
}

and use it in app.tsx via window.Echo

window.Echo.channel('messenger').listen('MessageSent', (e: any) => {
    console.log(e);
});
Sign up to request clarification or add additional context in comments.

Comments

2

I've added the following to my global.d.ts file

...
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

declare global {
  interface Window {
    ...
    
    Pusher: typeof Pusher;
    Echo : Echo;
}
....

}

1 Comment

Thanks, this has worked for as i wanted to have types and not add any
0

Use it as follows to get a completion
1. create global.d.ts at resources/js with the following

import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

declare global {
    interface Window {
        Pusher: Pusher;
        // replace "pusher" with "ably", "reverb" or whatever you're using
        Echo: Echo<"pusher">; 
    }
}

2. access it via window.Echo and you'll get a completion listenter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.