In this episode, we're diving into getting Laravel Echo working in a Vue CLI-based project so that your Vue frontend can respond to real-time events broadcasted from your Laravel backend using WebSockets.
We start completely from scratch—fresh Laravel install, database config done, but nothing else. We run through installing the Laravel WebSockets package (though you could use Pusher, too), publishing the config and migrations, and wiring up the broadcast driver to use the local WebSockets server. You'll see how to set up all the relevant config and environment variables to make your API ready for broadcasting.
On the Laravel side, we create a simple event (called Hello
) that implements the ShouldBroadcast
interface so we can test out event broadcasting, then set up an easy test route to trigger that event and see it show up in the Laravel WebSockets dashboard.
Once the backend is set up, we turn to the Vue client. We spin up a new Vue CLI app, install the laravel-echo
and pusher-js
packages, and show you exactly how to copy over and adapt the Echo initialization from Laravel’s boilerplate. For local dev, we walk through getting the needed environment variables and settings right—pointing to your local server and avoiding Pusher stats being sent to a third party.
Finally, you'll see how to listen for broadcasts of your Hello
event in your Vue app, console log the received data, and confirm that the real-time connection is working flawlessly. There’s discussion on how the setup would slightly change for production, but by the end, you’ll have a Vue app listening to real-time events from Laravel via your local WebSockets server!
It’s a longer one, but highly practical and hands-on—covering every step from infrastructure to code in both Laravel and Vue!