jorge_dev96
2 months ago
152
0
General

Problem with laravel-echo-server aurhentication

Posted 2 months ago by jorge_dev96

hello friends im using laravel-echo-server as alternative of pusher.js in this tutorial https://youtu.be/7r4s3kS5ItM

But when I tried to send data to socket.io this happens

bbRyazR6mzPsjb5fAAAC could not be authenticated to private-messages1

I checked several similar questions in StackOverflow

https://stackoverflow.com/questions/47950431/laravel-echo-server-404-when-trying-to-authenticate but none of these questions helped me

What I'm doing wrong? HELP :c

routes/channels.php

Broadcast::channel('messages.{id}', function ($user, $id) {
    dd($user->id, $id);

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

My event (with ShouldBroadcast )

class NewMessage implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $message;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message)
    {
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('messages.'.$this->message->to);
    }
    public function broadcastWith()
    {
        return ["message" => $this->message];
    }
}

config/app.php

I've uncommented this line App\Providers\BroadcastServiceProvider::class,

My vue component

....

Echo.private(`messages${this.user.id}`)
            .listen('NewMessage', (e)=>{
                this.handleIncoming(e.message);
    });

My bootstrap.js

...
import Echo from "laravel-echo"
window.io = require('socket.io-client');

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001' // this is laravel-echo-server host
});

The log of my error

[16:50:41] - Sending auth request to: http://newchat.test/broadcasting/auth

⚠ [16:50:42] - bbRyazR6mzPsjb5fAAAC could not be authenticated to private-messages1
{
    "message": "",
    "exception": "Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException",
    "file": "C:\laragon\www\newchat\vendor\laravel\framework\src\Illuminate\Broadcasting\Broadcasters\Broadcaster.php",
    "line": 81,
    "trace": [
        {
            "file": "C:\laragon\www\newchat\vendor\laravel\framework\src\Illuminate\Broadcasting\Broadcasters\RedisBroadcaster.php",
            "line": 69,
            "function": "verifyUserCanAccessChannel",
            "class": "Illuminate\Broadcasting\Broadcasters\Broadcaster",
            "type": "->"
        },
        {
            "file": "C:\laragon\www\newchat\vendor\laravel\framework\src\Illuminate\Broadcasting\BroadcastManager.php",
            "line": 320,
            "function": "auth",
            "class": "Illuminate\Broadcasting\Broadcasters\RedisBroadcaster",
            "type": "->"
        },
        {
            "file": "C:\laragon\www\newchat\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php",
            "line": 261,
            "function": "__call",
            "class": "Illuminate\Broadcasting\BroadcastManager",
            "type": "->"
        },
        {
            "file": "C:\laragon\www\newchat\vendor\laravel\framework\src\Illuminate\Broadcasting\BroadcastController.php",
            "line": 23,
            "function": "__callStatic",
            "class": "Illuminate\Support\Facades\Facade",
            "type": "::"
        },
        {
            "function": "authenticate",
            "class": "Illuminate\Broadcasting\BroadcastController",
            "type": "->"
        },
.....
}
Client can not be authenticated, got HTTP status 403

Please sign in or create an account to participate in this conversation.