Laravel - Broadcasting - Defining Authorization Callbacks
Next, we need to define the logic that will actually determine if the currently authenticated user can listen to a given channel. This is done in the routes/channels.php
file that is included with your application. In this file, you may use the Broadcast::channel
method to register channel authorization callbacks:
Broadcast::channel('orders.{orderId}', function ($user, $orderId) {
return $user->id === Order::findOrNew($orderId)->user_id;
});
The channel
method accepts two arguments: the name of the channel and a callback which returns true
or false
indicating whether the user is authorized to listen on the channel.
All authorization callbacks receive the currently authenticated user as their first argument and any additional wildcard parameters as their subsequent arguments. In this example, we are using the {orderId}
placeholder to indicate that the "ID" portion of the channel name is a wildcard.