Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pascal78's avatar
Level 33

TicTacToe Lesson 7 - Broadcasting to others

Just followed the course "Real Time Games With Laravel" to try Reverb

Great content, it inspired me and I'm trying to make a battleship game

I succeed to manage two boards and a bit more game logic than the Tic-Tac-Toe example it works pretty much

the channels / Reverb events / Echo listeners are the same as TicTacToe

However I got a question about broadcasting toOthers :

In GameController::update() method, although PlayerMadeMove Event is broadcasted with ->toOthers() both players, the emitter and his opponent, receive the event ! I inspected network tabs and putted a console.log() in the Echo..listen("PlayerMadeMove"..) It's displayed on both browsers at each move.

I restarted reverb server, queue, cleared cache and config There is apparently no typos ( I double checked the github repo )

I still can't explain why the emitter is aware of the event

Is it normal behavior or I am missing something ?

0 likes
3 replies
LaryAI's avatar
Level 58

It sounds like you're encountering an issue where the toOthers() method in Laravel's broadcasting system isn't behaving as expected. The toOthers() method is designed to broadcast an event to all subscribers of a channel except the current user. If both players are receiving the event, it suggests that the current user might not be correctly identified.

Here are a few steps to troubleshoot and potentially resolve the issue:

  1. Ensure the User is Authenticated: Make sure that the user is authenticated when broadcasting the event. The toOthers() method relies on the authenticated user to exclude them from the broadcast.

  2. Check the Broadcasting Configuration: Verify that your broadcasting configuration is set up correctly in config/broadcasting.php and that you are using the correct driver (e.g., Pusher, Redis).

  3. Verify the Event Class: Ensure that your event class implements the ShouldBroadcast interface and uses the BroadcastOn method correctly.

  4. Check the Event Broadcasting: Make sure you are using the toOthers() method correctly in your controller.

Here is an example of how you might structure your GameController::update() method and the event class:

GameController.php

use App\Events\PlayerMadeMove;
use Illuminate\Http\Request;

class GameController extends Controller
{
    public function update(Request $request)
    {
        // Your game logic here

        // Broadcast the event to others
        broadcast(new PlayerMadeMove($move))->toOthers();

        return response()->json(['status' => 'Move broadcasted']);
    }
}

PlayerMadeMove.php

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

class PlayerMadeMove implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public $move;

    public function __construct($move)
    {
        $this->move = $move;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('game.' . $this->move->game_id);
    }
}

JavaScript (Echo Listener)

Echo.private('game.' + gameId)
    .listen('PlayerMadeMove', (e) => {
        console.log('Player made a move:', e.move);
    });

Additional Debugging Steps:

  1. Check User Authentication: Ensure that the user is authenticated in your application. You can check this by logging the authenticated user in your controller method:

    \Log::info(auth()->user());
    
  2. Inspect the Network Tab: Double-check the network tab in your browser's developer tools to see if the event is being broadcasted multiple times or if there are any errors.

  3. Clear Cache: Run the following commands to clear any cached configurations:

    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    php artisan cache:clear
    

If after these steps the issue persists, it might be helpful to review the specific implementation details of your authentication and broadcasting setup. Additionally, ensure that your Echo client is correctly configured to use the same broadcasting driver as your Laravel application.

Please or to participate in this conversation.