Skip to content

[5.6] Channel classes#22583

Merged
taylorotwell merged 4 commits into
laravel:5.6from
tonysm:channel-classes
Jan 3, 2018
Merged

[5.6] Channel classes#22583
taylorotwell merged 4 commits into
laravel:5.6from
tonysm:channel-classes

Conversation

@tonysm

@tonysm tonysm commented Dec 31, 2017

Copy link
Copy Markdown
Contributor

Added

  • Artisan command to generate the channel classes

Changed

  • Allow passing channel authentication classes to clean up the channels file in the routes.

I have a demo app that uses the broadcasting feature and the channels file got a bit bloated. I figured we could a similar approach as the Phoenix version here and here.

This allowed me to convert this:

<?php

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('channels.{channel}', function (\App\User $user, \App\Channel $channel) {
    if (!$channel->exists || !$user->canJoin($channel)) {
        return false;
    }

    $user->joinChannel($channel);

    return [
        'id' => $user->id,
        'name' => $user->name,
        'avatar_path' => $user->avatar_path,
    ];
});

Broadcast::channel('companies.{company}', function (\App\User $user, \App\Company $company) {
    if (!$user->currentCompany || !$user->currentCompany->is($company)) {
        return false;
    }

    return [
        'id' => $user->id,
        'name' => $user->name,
        'avatar_path' => $user->avatar_path,
    ];
});

Into this:

<?php

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('channels.{channel}', \App\Broadcasting\ChatRoomChannel::class);
Broadcast::channel('companies.{company}', \App\Broadcasting\CompanyChannel::class);

And an example of the channel class:

<?php

namespace App\Broadcasting;

use App\User;
use App\Channel;

class ChatRoomChannel
{
    /**
     * Authenticates the channel.
     *
     * @param User $user
     * @param Channel $channel
     *
     * @return array|bool
     */
    public function join(User $user, Channel $channel)
    {
        if (!$channel->exists || !$user->canJoin($channel)) {
            return false;
        }

        $user->joinChannel($channel);

        return [
            'id' => $user->id,
            'name' => $user->name,
            'avatar_path' => $user->avatar_path,
        ];
    }
}

The idea is to allow the channels file to look more like the routes files (web and api) where we register the channel classes that will handle the channels authentication.

@GrahamCampbell
GrahamCampbell changed the base branch from 5.5 to 5.6 December 31, 2017 17:18
@GrahamCampbell GrahamCampbell changed the title [5.5] Channel classes [5.6] Channel classes Dec 31, 2017
@GrahamCampbell GrahamCampbell reopened this Jan 2, 2018
@taylorotwell
taylorotwell merged commit 5190646 into laravel:5.6 Jan 3, 2018
@tonysm
tonysm deleted the channel-classes branch January 3, 2018 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants