1
Q

Private channels require you to authorize that the currently authenticated user can actually listen on the channel. How is that accomplished?

A

This is accomplished by making an HTTP request to your Laravel application with the channel name and allowing your application to determine if the user can listen to that channel.
When broadcasting is enabled, Laravel automatically registers the /broadcasting/auth route to handle authorization requests. The /broadcasting/auth route is automatically placed within the web middleware group.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is the logic of an authorization of a user for a given channel handled?

A

This is done in the routes/channels.php file that was created by the install:broadcasting Artisan command. In this file, you may use the Broadcast::channel method to register channel authorization callbacks:

Broadcast::channel('orders.{orderId}', function (User $user, int $orderId) {
    return $user->id === Order::findOrNew($orderId)->user_id;
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The channel method accepts which two arguments?

A

The name of the channel and a callback which returns true or false indicating whether the user is authorized to listen to the channel.

All authorization callbacks receive the currently authenticated user as their first argument and any additional wildcard parameters as their subsequent arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you view a list of your application’s broadcast authorization callbacks using Artisan?

A

using the channel:list Artisan command:
php artisan channel:list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Private and presence broadcast channels authenticate the current user via your application’s default authentication guard. If the user is not authenticated, channel authorization is automatically denied and the authorization callback is never executed. However, you may assign multiple, custom guards that should authenticate the incoming request if necessary. How can you do that?

A
Broadcast::channel('channel', function () {
    // ...
}, ['guards' => ['web', 'admin']]);

just pass it as an array as an extra argument at the end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If your application is consuming many different channels, your routes/channels.php file could become bulky. So, instead of using closures to authorize channels, you may use channel classes. How can you generate a channel class using artisan and where will it be placed?

A

To generate a channel class, use the make:channel Artisan command. This command will place a new channel class in the App/Broadcasting directory.
php artisan make:channel ExampleChannel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you register your channel class?

A

You register your channel in your routes/channels.php file:
Broadcast::channel('examples.{example}, ExampleChannel::class);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Where can you place the authorization logic of a channel class?

A

You may place the authorization logic for your channel in the channel class’ join method. This join method will house the same logic you would have typically placed in your channel authorization closure. You may also take advantage of channel model binding.

public function join(User $user, Order $order): array|bool
{
    return $user->id === $order->user_id;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Once you have defined an event and marked it with the ShouldBroadcast interface, you only need to fire the event using what?

A

The event’s dispatch method. The event dispatcher will notice that the event is marked with the ShouldBroadcast interface and will queue the event for broadcasting:
OrderShipmentStatusUpdated::dispatch($order);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When building an application that utilizes event broadcasting, you sometimes need to broadcast an event to all subscribers to a given channel except for the current user. How can this be accomplished?

A

You may accomplish this using the broadcast helper and the toOthers method:
broadcast(new OrderShipmentStatusUpdated($update))->toOthers();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

If your application interacts with multiple broadcast connections and you want to broadcast an event using a broadcaster other than your default, how can you specify which connection to push an event to?

A

Using the via method:
broadcast(new OrderShipmentStatusUpdated($update))->via('pusher');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you broadcast a simple event to your application’s frontend without creating a dedicated event class?

A

To accommodate this, the Broadcast facade allows you to broadcast “anonymous events”:
Broadcast::on(‘orders.’.$order->id)->send();

Using the as and with methods, you may customize the event’s name and data:

Broadcast::on('orders.'.$order->id)
    ->as('OrderPlaced')
    ->with($order)
    ->send();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Once you have installed and instantiated Laravel Echo, how can you start listening for events that are broadcast from your Laravel application?

A

First, use the channel method to retrieve an instance of a channel, then call the listen method to listen for a specified event:

Echo.channel('orders.${this.order.id}')
         .listen('OrderShipmentStatusUpdated', (e) => {
             console.log(e.order.name);
         });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you listen for events on a private channel?

A

Use the private method instead. You may continue to chain calls to the listen method to listen for multiple events on a single channel:

Echo.private('orders.{$this.order.id}')
         .listen(/* ... */)
         .listen(/* ... */)
         .listen(/* ... */);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you stop listening to a given event without just outright leaving the channel?

A

Use the stopListening method:

Echo.private('orders.${this.order.id}')
         .stopListening('OrderShipmentStatusUpdated');
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which method lets you leave a channel?

A

Echo.leaveChannel('orders.${this.order.id}');

If you would like to leave a channel and also its associated private and presence channels, you may call the leave method with the same syntax instead.

17
Q

What are presence channels?

A

Presence channels build on the security of private channels while exposing the additional feature of awareness of who is subscribed to the channel. This makes it easy to build powerful, collaborative application features such as notifying users when another user is viewing the same page or listing the inhabitants of a chat room.
All presence channels are also private channels; therefore, users must be authorized to access them.

18
Q

When defining authorization callbacks for presence channels, you will not return true if the user is authorized to join the channel. What will you do instead?

A

Instead, you should return an array of data about the user. The data returned by the authorization callback will be made available to the presence channel event listeners in your JavaScript application. If the user is not authorized to join the presence channel, you should return false or null.

19
Q

How can you join a presence channel?

A

By using the Echo facade’s join method. It will return a PresenceChannel implementation which, along with exposing the listen method, allows you to subscribe to the here, joining, and leaving events.

20
Q

When will the here, joining, leaving, and error callbacks of a PresenceChannel joined with the Echo facade’s join method be called?

A

The here callback will be executed immediately once the channel is joined successfully, and will receive an array containing the user information for all of the other users currently subscribed to the channel. The joining method will be executed when a new user joins a channel, while the leaving method will be executed when a user leaves the channel. The error method will be executed when the authentication endpoint returns an HTTP status code other than 200 or if there is a problem parsing the returned JSON

21
Q

Sometimes you may want to broadcast an event to other connected clients without hitting your Laravel application at all. How can that be accomplished?

A

To broadcast client events, you may use Echo’s whisper method:

Echo.private('chat.${roomId}')
         .whisper('typing', {
             name: this.user.name
         });

To listen for client events, you may use the listenForWhisper method:

Echo.private('chat.${roomId}')
         .listenForWhisper('typing', (e) => {
             console.log(e.name);
         }=;

Broadcasting events like this can be particularly useful for things like “typing” notifications, where you want to alert users of your application that another user is typing a message on a given screen.