Laravel 5: Notifying Server Side Clients of Events With broadcast

April 14, 2018 —John Koster

The broadcast helper function is similar in function to the event helper function in that, if an event implements the "Illuminate\Contracts\Broadcasting\ShouldBroadcast" interface, it will dispatch the event to all server-side listeners and notify any client-side channel subscribers. However, the difference is that the broadcast helper function will return an instance of "Illuminate\Broadcasting\PendingBroadcast", which provides greater control over which client-side subscribers receive the event notification.

#Signature

The signature of the broadcast function is:

1function broadcast(
2 $event = null
3);

#Example Use

In the first example, we will create a simple broadcastable event and an Artisan command that we can invoke to send the event to our client side listeners. This will depend on the following files:

In app/Events/TestEvent.php:

1<?php
2 
3namespace App\Events;
4 
5use Illuminate\Broadcasting\Channel;
6use Illuminate\Queue\SerializesModels;
7use Illuminate\Foundation\Events\Dispatchable;
8use Illuminate\Broadcasting\InteractsWithSockets;
9use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10 
11class TestEvent implements ShouldBroadcast
12{
13 use Dispatchable,
14 InteractsWithSockets,
15 SerializesModels;
16 
17 /**
18 * Get the channels the event should broadcast on.
19 *
20 * @return \Illuminate\Broadcasting\Channel|array
21 */
22 public function broadcastOn()
23 {
24 return new Channel('test');
25 }
26}

In app/Console/Commands/TriggerEvent.php:

1<?php
2 
3namespace App\Console\Commands;
4 
5use App\Events\TestEvent;
6use Illuminate\Console\Command;
7 
8class TriggerEvent extends Command
9{
10 /**
11 * The name and signature of the console command.
12 *
13 * @var string
14 */
15 protected $signature = 'trigger:event';
16 
17 /**
18 * The console command description.
19 *
20 * @var string
21 */
22 protected $description = 'Triggers our test event.';
23 
24 /**
25 * Execute the console command.
26 *
27 * @return mixed
28 */
29 public function handle()
30 {
31 // Notify client-side listeners with the
32 // Laravel `event` helper function.
33 event(new TestEvent());
34 
35 // Notify client-side listeners with the
36 // Laravel `broadcast` helper function.
37 broadcast(new TestEvent());
38 }
39}

In resources/assets/js/app.js:

1 
2require('./bootstrap');
3 
4Echo.channel('test')
5 .listen('TestEvent', (e) => {
6 console.log('TestEvent', e);
7 });

When we invoke the TriggerEvent command like so:

1php artisan trigger:event

we would see results similar to the following in the browser console (the following results were taken from the Google Chrome console with an app.js file containing comments, your exact results will change depending on browser and your specific app.js file):

1app.js:47887 TestEvent []
2app.js:47887 TestEvent []

As you can see, when broadcasting over public channels there is no advantage to using the broadcast helper function over the event helper function; this is because the TestEvent we are firing broadcasts itself to all users on a public channel. If we were to be using private channels, we could notify all users except the current user by using the broadcast helper function like so:

1// Only notify other users of an event.
2broadcast(new TestEvent())->toOthers();

Besides the ability to exclude the currently authenticated user, there is really no difference to using the broadcast helper function over the event helper function.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.