php
20 lines · 3 steps
Authorizing presence channels in Laravel
A broadcast channel callback gates access to a team channel and returns presence data for members.
Explained by
highlit
1<?php
2
3namespace App\Broadcasting;
4
5use App\Models\Team;
6use App\Models\User;
7use Illuminate\Support\Facades\Broadcast;
8
9Broadcast::channel('team.{team}', function (User $user, Team $team) {
10 if (! $user->belongsToTeam($team)) {
11 return false;
12 }
13
14 return [
15 'id' => $user->id,
16 'name' => $user->name,
17 'avatar' => $user->avatar_url,
18 'role' => $team->roleFor($user),
19 ];
20});
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Channel callbacks double as gatekeepers: returning false denies subscription while a truthy value grants it.
- 2On presence channels, the returned array becomes the member's public identity shared with everyone else in the channel.
- 3Route-model binding resolves the channel's {team} placeholder into a full model before your logic runs.
Related explainers
ruby
class ToastBroadcaster include ActionView::RecordIdentifier def self.broadcast_to(user, message:, type: :notice)
How Turbo Stream toasts broadcast in Rails
turbo-streams
service-object
real-time
Intermediate
6 steps
javascript
const ROLE_PERMISSIONS = { admin: ['users:read', 'users:write', 'billing:read', 'billing:write'], manager: ['users:read', 'billing:read'], member: ['users:read'],
Role-based permissions middleware in Express
authorization
middleware
rbac
Intermediate
9 steps
php
<?php namespace App\Console\Commands;
Releasing stale document locks in Laravel
artisan-command
transactions
row-locking
Intermediate
6 steps
php
<?php namespace App\Providers;
Subdomain multi-tenancy routing in Laravel
multi-tenancy
service-container
route-binding
Advanced
7 steps
php
<?php namespace App\Http\Middleware;
Resolving the current team in Laravel middleware
middleware
multi-tenancy
cookies
Intermediate
8 steps
php
<?php namespace App\Http\Controllers;
Broadcasting typing indicators in Laravel
websockets
authorization
presence-channels
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/authorizing-presence-channels-in-laravel-explained-php-eafd/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.