title: Implementing Laravel 8 broadcasting with Pusher and Laravel Echo in a Vue frontend
body: <p>I am trying to implement event broadcasting and notification with laravel. The goal is to broadcast a private message to a logged in users via notifications.</p>
  <p>I created this event, see code below:</p>
  <pre><code>&lt;?php
  
  namespace App\Events;
  
  use App\Models\User;
  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 TradingAccountActivation implements ShouldBroadcast
  {
      use Dispatchable, InteractsWithSockets, SerializesModels;
  
       /**
       * The authenticated user.
       *
       * @var \Illuminate\Contracts\Auth\Authenticatable
       */
      public $user;
      public $message;
  
      /**
       * Create a new event instance.
       *
       * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
       * @return void
       */
      public function __construct(User $user)
      {
          $this-&gt;user = $user;
          $this-&gt;message = &quot;{$user-&gt;first_name} is ready to trade&quot;;
      }
  
     
      /**
       * Get the channels the event should broadcast on.
       *
       * @return \Illuminate\Broadcasting\Channel|array
       */
      public function broadcastOn()
      {
          return new PrivateChannel('user.'.$this-&gt;user-&gt;id);
      }
  
  }
  
  </code></pre>
  <p>The event is to be fired for every newly verified user, so I put the event in the project's email verification controller, see code below:</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Auth;
  
  use App\Http\Controllers\Controller;
  use App\Providers\RouteServiceProvider;
  use Illuminate\Auth\Events\Verified;
  use App\Events\TradingAccountActivation;
  use Illuminate\Foundation\Auth\EmailVerificationRequest;
  class VerifyEmailController extends Controller
  {
      /**
       * Mark the authenticated user's email address as verified.
       *
       * @param  \Illuminate\Foundation\Auth\EmailVerificationRequest  $request
       * @return \Illuminate\Http\RedirectResponse
       */
      public function __invoke(EmailVerificationRequest $request)
      {
          if ($request-&gt;user()-&gt;hasVerifiedEmail()) {
              return redirect()-&gt;intended(RouteServiceProvider::HOME.'?verified=1');
          }
  
          if ($request-&gt;user()-&gt;markEmailAsVerified()) {
              event(new Verified($request-&gt;user()));
          } 
  
          event(new TradingAccountActivation($request-&gt;user()));
          return redirect()-&gt;intended(RouteServiceProvider::HOME.'?verified=1');
             
      }
  }
  </code></pre>
  <p>At this point the event fails with this error message:</p>
  <pre><code>ErrorException
  array_merge(): Expected parameter 2 to be an array, null given
  
  </code></pre>
  <p>The stack trace points to pusher on the line with an asterisk:</p>
  <pre><code>Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
  C:\xampp\htdocs\spa\vendor\pusher\pusher-php-server\src\Pusher.php:391
  
          $path = $this-&gt;settings['base_path'].'/events';
  
           // json_encode might return false on failure
  
          if (!$data_encoded) {
  
              $this-&gt;log('Failed to perform json_encode on the the provided data: {error}', array(
  
                  'error' =&gt; print_r($data, true),
  
              ), LogLevel::ERROR);
  
          }
  
           $post_params = array();
  
          $post_params['name'] = $event;
  
          $post_params['data'] = $data_encoded;
  
          $post_params['channels'] = array_values($channels);
  
      *    $all_params = array_merge($post_params, $params);
  
           $post_value = json_encode($all_params);
  
           $query_params['body_md5'] = md5($post_value);
  </code></pre>
  <p>Laravel Telescope confirms that the event failed with these details:</p>
  <p>Job Details
  Time    April 6th 2021, 9:05:01 AM (1:56m ago)
  Hostname    Adefowowe-PC
  Status  failed
  Job App\Events\TradingAccountActivation
  Connection  sync
  Queue<br />
  Tries   -
  Timeout -
  Tags    App\Models\User:75Auth:75failed
  Authenticated User
  ID  75
  Email Address   j@doe.com</p>
  <p>along with the data the event was meant to emit:</p>
  <pre><code>{
  &quot;event&quot;: {
  &quot;class&quot;: &quot;App\Events\TradingAccountActivation&quot;,
  &quot;properties&quot;: {
  &quot;user&quot;: {
  &quot;id&quot;: 75,
  &quot;uuid&quot;: &quot;75da67ef-d6f8-4cd0-9d57-e1dcf66c1f5e&quot;,
  &quot;first_name&quot;: &quot;John&quot;,
  &quot;last_name&quot;: &quot;Doe&quot;,
  &quot;mobile_phone_number&quot;: &quot;08033581133&quot;,
  &quot;verification_code&quot;: null,
  &quot;phone_number_isVerified&quot;: 0,
  &quot;phone_number_verified_at&quot;: null,
  &quot;email&quot;: &quot;j@doe.com&quot;,
  &quot;email_verified_at&quot;: &quot;2021-04-06T08:04:49.000000Z&quot;,
  &quot;created_at&quot;: &quot;2021-04-06T08:03:56.000000Z&quot;,
  &quot;updated_at&quot;: &quot;2021-04-06T08:04:49.000000Z&quot;
  },
  &quot;message&quot;: &quot;John is ready to trade&quot;,
  &quot;socket&quot;: null
  }
  },
  &quot;tries&quot;: null,
  &quot;timeout&quot;: null,
  &quot;connection&quot;: null,
  &quot;queue&quot;: null,
  &quot;chainConnection&quot;: null,
  &quot;chainQueue&quot;: null,
  &quot;chainCatchCallbacks&quot;: null,
  &quot;delay&quot;: null,
  &quot;afterCommit&quot;: null,
  &quot;middleware&quot;: [
  ],
  &quot;chained&quot;: [
  ]
  }
  
  </code></pre>
  <p>Curiously, it appears the broadcast channel was created and a connection established despite the event failing. Refreshing the error page appears to proceed with the next action of the controller which is to redirect to the dashboard of the authenticated user. At this point a broadcast connection is established. See Laravel Telescope details below with the payload:</p>
  <pre><code>Request Details
  Time    April 6th 2021, 10:02:38 AM (10s ago)
  Hostname    Adefowowe-PC
  Method  POST
  Controller Action   \Illuminate\Broadcasting\BroadcastController@authenticate
  Middleware  auth:web
  Path    /broadcasting/auth
  Status  302
  Duration    1043 ms
  IP Address  127.0.0.1
  Memory usage    12 MB
  Payload
  Headers
  Session
  Response
  {
  &quot;socket_id&quot;: &quot;131623.12865758&quot;,
  &quot;channel_name&quot;: &quot;private-user.${this.user.id}&quot;
  }
  </code></pre>
  <p>Since the event failed, I didn't expect expect a broadcast channel to be established or for the subsequent listeners and notifications broadcast message to fire.</p>
  <p>I can't figure out why the event failed, i.e. how to deal with the exception &quot;array_merge(): Expected parameter 2 to be an array, null given&quot;, or how to fix it.</p>
  <p>Or if it has anything to do with subsequent codes for receiving/logging/displaying the broadcast message.</p>
  <p>Thanks.</p>