1

In my ChatEvent i called function

public function broadcastOn()
{
    return new Channel('chat');
}

And in app.js i have Echo.

Echo.channel('chat')
        .listen('ChatEvent', (e) => {
            this.chat.message.push(e.message);
            console.log(e);
    })

It works pretty well. But, when i change Channel to PrivateChannel in function broadcastOn() and in app.js I change

Echo.private('chat')
        .listen('ChatEvent', (e) => {
            this.chat.message.push(e.message);
            console.log(e);
    })

I have error POST broadcasting/auth 403 (Forbidden) @@ And Can I use Channel instead of PrivateChannel?

Quan Nguyen
  • 99
  • 3
  • 11

4 Answers4

1

Like mentioned in document you have to define the authorization rule for this private channel

https://laravel.com/docs/5.5/broadcasting#authorizing-channels

edit your routes/channels.php file

Broadcast::channel('chat', function ($user) {
    return true; //for public access
    // or
    return $user->can('chat'); //using gate
});

else use the channel for public access

Praveen Tamil
  • 1,076
  • 11
  • 22
1

When you use Private or PresenceChannel, Fix Error 403 /broadcasting/auth with Laravel version > 5.3 & Pusher, you need change your code in resources/assets/js/bootstrap.js with

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your key',
    cluster: 'your cluster',
    encrypted: true,
    auth: {
        headers: {
            Authorization: 'Bearer ' + YourTokenLogin
        },
    },
});

And in app/Providers/BroadcastServiceProvider.php change by

Broadcast::routes()

with

Broadcast::routes(['middleware' => ['auth:api']]);

or

Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT

it worked for me, and hope it help you.

Alex
  • 3,646
  • 1
  • 28
  • 25
0

Try this

in Chat event add

public function broadcastAs()
{
    return 'new.chat';
}

in your javascript file

Echo.channel('chat')
        .listen('.new.chat', (e) => {
            console.log(e);
    })
derrickrozay
  • 1,048
  • 3
  • 15
  • 37
0

if use api

window.Echo.connector.options.auth.headers['Authorization'] = `Bearer ${user.api_token}`;

if use session auth without top code