Manh Nguyen
Manh Nguyen

Reputation: 439

Laravel Echo 500 error (broadcasting/auth)?

have a nice day !

I have a problem: I set up Laravel Echo & Pusher but got this error, have no idea to resolve :(

enter image description here

I checked my app-key, app-cluster but all are correct.

Can someone help me?

app.js

const app = new Vue({
    el: '#app',
    data: {
        messages: []
    },
    methods:{
        addMessage(message){
            this.messages.push(message);
            axios.post('/messages', message).then(response => {
               console.log(response);
            });
        }
    },
    created(){
        axios.get('/messages').then(response => {
            this.messages = response.data;
        });

        Echo.channel('chatroom')
            .listen('MessageEvent', (e) => {
                console.log(e);
            });
    }
})

bootstrap.js

import Echo from 'laravel-echo'

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

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '************',
    cluster: 'ap1',
    encrypted: false
});

MessageEvent

use Dispatchable, InteractsWithSockets, SerializesModels;
public $message, $user;

public function __construct(Message $message, User $user)
{
    $this->message = $message;
    //query
    $this->user = $user;
}

public function broadcastOn()
{
    return new PresenceChannel('chatroom');
}

channels.php

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

Broadcast::channel('chatroom', function ($user, $id) {
    return $user;
});

Upvotes: 4

Views: 8757

Answers (4)

Franz
Franz

Reputation: 354

if you're working on localhost make sure that your .env file is set up properly

try setting

APP_URL=http://localhost
DB_HOST=localhost

and run

php artisan config:cache

hope this will help you.

Upvotes: 0

Sambit Mohapatra
Sambit Mohapatra

Reputation: 153

Remove the $id as you are not passing from the event

Broadcast::channel('chatroom', function ($user) {
  return true;
});

Upvotes: 3

Alex
Alex

Reputation: 4266

Error 403 or 500 /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.

Upvotes: 7

jay.codefarm
jay.codefarm

Reputation: 11

I think you need to give an authentic point if u have used laravel echo just goto Resources/assets/js/bootstrap.js

Just add the following line inside the window

Echo = new Echo({
authEndpoint : 'http://localhost/projectName/public/broadcasting/auth',
});

Upvotes: 1

Related Questions