Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Anonymous Broadcasting in Laravel 11.5

Premium
4 min read

Laravel 11.5 has just been released, and it added anonymous broadcasting. What is it? Let's take a look.

This message is triggered by a private anonymous event, so let's see how it's done.


Global Anonymous Events

We want to start by re-creating our first example from the previous lesson—a global event that will inform all users about system maintenance. We will use the same event and channel, but this time, we will make it anonymous.

To create a channel for this, we will modify the routes/channels.php file:

// ...
 
Broadcast::channel('system-maintenance', function () {
// Public Channel
});

Then, we want to create a new command to trigger this event:

php artisan make:command SystemNotifyMaintenanceCommand

And modify the command file:

app/Console/Commands/SystemNotifyMaintenanceCommand.php

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Broadcast;
 
class SystemNotifyMaintenanceCommand extends Command
{
protected $signature = 'system:notify-maintenance';
 
protected $description = 'Command description';
 
public function handle(): void
{
$time = $this->ask('When should it happen?');
 
Broadcast::on('system-maintenance')
->as('App\\Events\\SystemMaintenanceEvent')
->with([
'time' => $time
])
->via('reverb')
->send();
}
}

Next, we will add a...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

DS
Dmytro Sakharuk ✓ Link copied!

It seems unnecessary to add this for a public channel

Broadcast::channel('system-maintenance', function () {
	return true;
});
M
Modestas ✓ Link copied!

In a way - yes, but on quick look - it makes it clear that everyone can access it. So it's just for readability

DS
Dmytro Sakharuk ✓ Link copied!

Perhaps it would be more appropriate to list public channels in the form of comments, because even if you return false, the message will still be delivered, which is somewhat misleading, implying that we can control access to the public channel.

M
Modestas ✓ Link copied!

At this stage - I might agree with you. Indeed, a better choice would be a comment, thanks :)

�S
Đào Sơn ✓ Link copied!

window.Echo.private('App.Models.User.' + {{ auth()->id() }})

if i use Vue or Nuxt , how to use this code ? Thank

DS
Dmytro Sakharuk ✓ Link copied!

You can add something like the following to the Blade template:

<script>
	window.authUserId = {{ auth()->id() }};
</script>

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.