Skip to content
Luca Ubiali Web Developer

Real Time Games With Laravel - Quiet as a Whisper

September 3rd, 2024
Continuous Learning

Episode - https://laracasts.com/series/real-time-games-with-laravel/episodes/7


I was waiting to watch this episode to see “whispers“ in action. As of now the app is slow at sending updates from one player to the other since any move must to go through the server. Whispers allow direct communication between clients.

1<script>
2const channel = Echo.join()
3 //...
4 .listenForWhisper('PlayerMadeMode', ({state}) => {
5 boardState.value = state;
6 checkForVictory();
7 });
8 
9 
10const fillSquare = () => {
11 //...
12 
13 channel.whisper('PlayerMadeMove', {
14 state: boardState.value,
15 });
16}
17</script>

When listening for an event through Echo, we can now use listenForWhisper instead of listen.

The PresenceChannel instance returned by Echo can be used to whisper an event whenever a user makes a move. This is enough to get instant update across browser with direct web sockets communication.

With whispers in place, all the code related to backend PlayerMadeMove event is no longer needed and can be removed.

Whisper is the perfect fit in this scenario as there’s basically no business logic related to that event. If there was more complex logic, maybe data validation and more in-depth interactions with the database, standard events would still be needed.