Broadcast channels not being registered in Laravel

577 Views Asked by At

I've been stuck with Broadcasting on private channel in authentication part. What I have done is, I made custom authEndpoint in Echo server

"authEndpoint": "/broadcastAuth",

Controller:

public function broadcastAuth(Request $request){
    $channel = $this->normalizeChannelName($request->channel_name);
    $broadcaster = new CustomBroadcaster();
    $channelAuth = $broadcaster->verifyUserCanAccessChannel($request, $channel);
    if($channelAuth){
        return true;
    }
    return response()->json([
        'message' => 'Not allowed'
    ], 403);
}

I have made a custom broadcaster class that extends Illuminate\Broadcasting\Broadcasters\Broadcaster so that I can override verifyUserCanAccessChannel method My verifyUserCanAccessChannel method:

public function verifyUserCanAccessChannel($request, $channel)
{
    Log::info($this->channels);
    foreach ($this->channels as $pattern => $callback) {
        if (! $this->channelNameMatchesPattern($channel, $pattern)) {
            continue;
        }

        $parameters = $this->extractAuthParameters($pattern, $channel, $callback);
        $handler = $this->normalizeChannelHandlerToCallable($callback);

        if ($result = $handler($request->user(), ...$parameters)) {
            return $this->validAuthenticationResponse($request, $result);
        }
    }

    throw new AccessDeniedHttpException;
}

Here I have logged $this->channels to get the registered channels from routes/channel.php But the result is empty. Here is my BroadcastServiceProvider:

public function boot()
{
    require base_path('routes/channels.php');
}

Here is my channel.php:

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});
Broadcast::channel('post-comment.{id}', function ($user, $id){
    \Illuminate\Support\Facades\Log::info($user);
    return true;
});

And I have uncommented both in config/app.php

Illuminate\Broadcasting\BroadcastServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,

What could be the mistake here. Please help me figure this out. Thank you in advance.

0

There are 0 best solutions below