Search code examples
laravel-echopusher-js

Error Echo is not Defined


I'm using pusher and have been trying to configure Laravel Echo as guided in the docs. I have added the following code to bootstrap.js

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '###key###',
    cluster: 'ap2',
    encrypted: true
});

After compiling and running the code and I try to use Echo to subscribe to a channel, I get the console error that Echo is not defined.

I suspect that when Laravel Mix (5.4) is compiling the assets, it is not including bootstrap.js where Echo is defined. To confirm this, I check the contents of the compiled app.js and sure enough, the content from the bootstrap.js file are not there.

So, how do I ensure that bootstrap.js is also compiled, or how do I go about all this?


Solution

  • I detected why bootstrap.js was not being compiled together with the rest of the code. I will write it here because it may be benefit someone else.

    The problem was that in the process of making edits to app.js, I had removed the line require('./bootstrap'), which is the line that imports bootstrap.js (probably I thought it is meant to include Twitter Bootstrap).

    Just that. It now works well. HTH!