Episode - https://laracasts.com/series/real-time-games-with-laravel/episodes/2
Event Broadcasting is set up in this episode. This feels like a kata again and personally is one where I usually trip up on one of the steps then spend way too much time figuring out what I forgot. So this is a nice refresher.
php artisan install:broadcasting
php artisan make:event
on the backend
edit the event to broadcast on the desired channel
fire the event from somewhere in the code
on the frontend
check that
echo.js is
set up correctly and referenced inbootstrap.js
listen for the event through Echo
Define the channel in
routes/channels.php
php artisan reverb:start
php artisan queue:listen
I only recently moved to Reverb. Before I was using Pusher which didn’t need this step. Right now I constantly forget about this one
didn’t know about the
listen
parameter that will reload the queue when the code changes. Nice!
And to round off the lesson some Vue.js refresh:
1<script> 2const props = defineProps(['games']); 3 4const games = ref(props.games.data); 5 6Echo.private('lobby') 7 .listen('GameJoined', (event) => { 8 games.value = games.value.filter((game) => game.id !== event.game.id); 9 10 if(games.length < 5) {11 router.reload({12 only: ['games'],13 onSuccess: () => games.value = props.games.data14 });15 }16 });17</script>
When using a ref in the script tag, remember to use .value
.
When using Inertia reload
method it’s possible to request just a list of props to reload, instead of all of them.
When reload
finishes successfully, the internal state of refs is not updated automatically, so will have to be done explicitly.