Search code examples
phplaravelpusherlaravel-websockets

"Argument 4 passed to Pusher\Pusher::trigger() must be of the type array, null given" when dispatching a default ShouldBroadcast Event


I am trying to follow along with this tutorial: https://www.youtube.com/watch?v=AUlbN_xsdXg which is very straight forward and simple, but unfortunately does not work for me. Based on the comments others do not seem to be having this issue.

This is what I've done so far:

  1. Installed "beyondcode/laravel-websockets": "^1.13" and "pusher/pusher-php-server": "7.0" (First tried the current version 7.2 but there was no difference in the error) and did composer updates
  2. Uncommented App\Providers\BroadcastServiceProvider in config/app.php
  3. Changed .env to BROADCAST_DRIVER=pusher and Modified config/broadcasting.php
'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'), // These env vars have config:cache'd dummy values
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'useTLS' => false,
                'encrypted' => false,
                'host' => '127.0.0.1',
                'port' => '6001',
                'scheme' => 'http',
            ],
        ],
  1. Published websockets.php and the migration files/performed the migration
  2. Created the dummy Event class and added 'implements ShouldBroadcast'
<?php

namespace App\Events;

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

class PlaygroundEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('public.playground.1');
        // return new PrivateChannel('channel-name');
    }
}
  1. Added a test in my web.php routes file
Route::get('/playground', function(){
    event(new \App\Events\PlaygroundEvent());
    return null;
});
  1. When visiting that page I encounter the error:
Argument 4 passed to Pusher\Pusher::trigger() must be of the type array, null given, called in [...]vendor\laravel\framework\src\Illuminate\Broadcasting\Broadcasters\PusherBroadcaster.php on line 113

If you search for that error you will find like 2 results that are both unresolved. Not sure where to mention this but the WebSockets Dashboard appears to work fine.

Environment: PHP Version 7.4.5, Windows 10 64, "laravel/framework": "^7.0", "beyondcode/laravel-websockets": "^1.13", "pusher/pusher-php-server": "7.0" and 7.2 while trying to fix.

Since there's so little out there for this error I'm not sure where to go next, except here of course. Any help appreciated, thank you.

Edit: The stacktrace: https://justpaste.it/9lwe3


Solution

  • Try adding a payload to the event:

    public function broadcastWith() {
        return [
            "foo" => "bar"
        ];
    }
    

    Alternatively, upgrading to Laravel 8+ should solve this issue.