Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

havvtom's avatar

Broadcasting on private channels

My app is giving a POST http://127.0.0.1:8000/broadcasting/auth 403 (Forbidden) error when I use a private channel. Its working perfectly with public channel. The error only shows when I change to a private channel. I am logged in, meaning the $user argument required in channel.php is there. I have commented the line in the closure and put return true. Is there anything I am missing?

0 likes
11 replies
marbobo's avatar

you should provide some codes in order for us to guess what is the issue. okay i guess you have uncommented BoardcastServiceProvider on config/app.php

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,  // <--- you should uncomment this.
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Barryvdh\Debugbar\ServiceProvider::class,
1 like
GankSt's avatar

@marbobo It takes me two days to face a 403 error on a Private channel. Then I saw this comment, and the problem was solved. Thanks a lot, man.

havvtom's avatar
class TaskAdded implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $task;

    public function __construct($task)
    {
        $this->task = $task;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        
        return new PrivateChannel('task'.$this->task->project_id);
    }
}
havvtom's avatar

hi KevDev. Above is my event. Below is my part of my config/app.php

  * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

bootstrap.js

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true
});

channels.php

Broadcast::channel('task.{projectId}', function ($user, $id) {

   return true;
    // return (int) $user->id === (int) $id;
});

my vue component

 created(){
           
           axios.get(/tasks/+this.project.id).then(response=>{
            this.tasks = response.data;
           });
           window.Echo.private('task'+this.project.id)
            .listen('TaskAdded', e=>{
                console.log(e.task.description);
                // this.tasks.push(e.task.description);
            })
Tippin's avatar

What about your channels.php file. Did you add the channel to the channel routing?

Broadcast::channel('task.{project_id}' , function($user, $project_id) {
   return true; //run logic to see if user has permission to access project channel
});
Tippin's avatar

Sorry posted right as you did. Two things though I notice...projectId must also be $projectId in method. Also, where you define PrivateChannel,

    public function broadcastOn()
    {
        return new PrivateChannel('task.'.$this->task->project_id);
    }

Be sure to use the dot in the string name for channel if you call it that way in channels.php

havvtom's avatar

Thanks @tippin . I have fixed that, but I am still getting the error.

Tippin's avatar

In your providers folder in app, do you also have un-commented the broadcast routes?

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes();

        require base_path('routes/channels.php');
    }
}
havvtom's avatar

Yes it is uncommented

     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes();

        require base_path('routes/channels.php');
    }
}
Tippin's avatar
Tippin
Best Answer
Level 13

Also add dot to javascript private channel name

axios.get(/tasks/+this.project.id).then(response=>{
    this.tasks = response.data;
});
window.Echo.private('task.'+this.project.id)
.listen('TaskAdded', e=>{
    console.log(e.task.description);
    // this.tasks.push(e.task.description);
})

Please or to participate in this conversation.