Laravel Echo Server — Broadcast Channel Classes

Dennis Smink
3 min readJun 12, 2018

--

In my previous article I talked about private channels, and how this could clutter up your channels.php file if you have a lot of logic inside these routes.

A example broadcasting channel class.

With this article I would like to introduce you to broadcasting channels, which allow you to move the ‘channel logic’ to its own custom class.

If you do not have any knowledge of Laravel echo yet, I recommend you to read my previous article about setting up Laravel echo server, I assume you already have a Laravel installation running with Laravel echo server.

Lets get started

We can use this command to create broadcasting channels:

$ php artisan make:channel ExampleBroadcastChannel

This will create the channel class in side App/Broadcasting folder named ExampleBroadcastChannel.php. This is the file we will be working in.

If you open up this file, you will see a constructor and a join method. We will be using the join method, you can move your channel logic from your channels.php file inside this class respectively.

The join method is called whenever a user is requesting access to this channel, so in this function we will writing our logic to determine wether a user can, or cannot access this channel.

This looks like following:

/**
* Authenticate the user's access to the channel.
*
* @param \App\User $user
* @return array|bool
*/
public function join(User $user)
{
//
}

Alright, I will show you an example channel logic that I have, I have this in my routes/channels.php file:

Broadcast::channel('teams.{teamId}', function ($user, $teamId) {
return $user()->teams()->find($teamId) ? true : false;
});

This will determine if a user contains a team (you can execute this query however you like, its just an example and the goal is to return true or false ) and returns wether a user is allowed to connect this channel.

With our newly created channel class, lets move this to the join function inside the ExampleBroadcastChannel.php file:

/**
* Authenticate the user's access to the channel.
*
* @param \App\User $user
* @return array|bool
*/
public function join(User $user, $teamId)
{
return $user->teams()->find($teamId) ? true : false;
}

Also update your channels.php file to accept the class you just created:

use App\Broadcasting\ExampleBroadcastChannel;

Broadcast::channel('teams.{teamId}', ExampleBroadcastChannel::class);

Additionally, you can also use route model binding if you use the keys correctly in your channels.php file, we will also bind the class we created to the channel route:

use App\Broadcasting\ExampleBroadcastChannel;

Broadcast::channel('teams.{team}', ExampleBroadcastChannel::class);

You can also fully type the namespace of the class:

Broadcast::channel('teams.{team}', \App\Broadcasting\ExampleBroadcastChannel::class);

And then, inside the ExampleBroadcastChannel.php file write our your logic (once again, this is completely up to you how you want to handle this logic):

/**
* Authenticate the user's access to the channel.
*
* @param \App\User $user
* @return array|bool
*/
public function join(User $user, Team $team)
{
return $team->users->contains($user);
}

As you can see, rewriting the channel logic to its own class makes the channels.php file way cleaner just like web.php file for example, no more closures!

For more information about the channel classes I will refer you to the Laravel documentation:

I hope this article was of any use for you, if you have any questions leave a reply below.

Thank you for reading!

Note: please bare in mind that I am a Dutch developer, English is not my native language so this article could contain any grammatical errors.

--

--