Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Channel classes #22583

Merged
merged 4 commits into from
Jan 3, 2018
Merged

[5.6] Channel classes #22583

merged 4 commits into from
Jan 3, 2018

Conversation

tonysm
Copy link
Contributor

@tonysm tonysm commented Dec 31, 2017

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.

None yet

3 participants