In this episode, we're building a simple real-time feature between Laravel and a Nuxt client. First, we set up a button in the Nuxt app that will send a request to a new API route in Laravel. That route, when hit, will broadcast an event using Laravel's broadcasting system (here, Reverb is assumed, but the setup is similar for others).
We start by installing the required API functionality in our Laravel app, which involves Sanctum and some migrations. Then, we set up an endpoint called /api/real-time
that will broadcast a new ExampleEvent
to the public
channel every time it is hit. On the Nuxt side, we hardcode the URL for now and make a POST request when our button is clicked.
Next, we set up Laravel Echo (already installed as a plugin) in our Nuxt client to listen on the same public
channel for the ExampleEvent
. Initially, we just log incoming events, but soon after, we enhance the UX: we push event strings into a reactive array and list them live on the page, so whenever the button is clicked, every connected client instantly sees the new string.
Finally, we clean up some TypeScript errors by properly typing the window
object and the event payload. The end result is a real-time flow: click a button in one browser, and everyone with the app open sees the update immediately!
Next up, we'll tidy up our config values so that things are less hardcoded and the app is more robust.