5

I am trying to authenticate the laravel echo using a private channel. Every time I am getting "could not be authenticated to private-basket_details.1 " . "basket_details" this is the channel name and 1 is the parameter.

When I use a public channel everything works perfectly. So I think there are some issues with the auth part.

This is my error log

⚠ [14:42:55] - 5eKrT28nX7uLHEkLAAAG could not be authenticated to private-basket_details.1
{
    "message": "",
    "exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
    "file": "/var/www/html/Laravel/uneek_clothing/trunk/public_html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
    "line": 179,
    "trace": [
        {
            "file": "/var/www/html/Laravel/uneek_clothing/trunk/public_html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 612,
            "function": "match",
            "class": "Illuminate\Routing\RouteCollection",
            "type": "->"
        },
        {
            "file": "/var/www/html/Laravel/uneek_clothing/trunk/public_html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 601,
            "function": "findRoute",
            "class": "Illuminate\Routing\Router",
            "type": "->"
        },
        {

This is my Event file.

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use App\Basket;
use App\User;

class UpdateWebOrderDetailsToBasket implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $basket;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Basket $basket)
    {
        $this->basket = $basket;
    }

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

}

This is my channels.php

<?php
// use Illuminate\Broadcasting\PrivateChannel;

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

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

// Broadcast::PrivateChannel('basket.{basketId}', function ($user, $basketId) {
//     return true;
// });

// Broadcast::channel('basket_details.1', function ($user, $basketId) {
//     return true;
// });

Broadcast::channel('basket_details.{basketId}', function ($user, $basketId) {
    return true;
});

// Broadcast::channel('private-basket_details.*', function ($user, $basketId) {
//     return true;
// });

This is my js file

Echo.private('basket_details.1')
    .listen('.App\Events\UpdateWebOrderDetailsToBasket', (e) => {
      console.log("channel started here");
    }); 

This is my BroadcastServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

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

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

This is my laravel-echo-server.json

{
    "authHost": "http://192.168.1.120:1002",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "be17370a567448f7",
            "key": "7af893ebfa188744e6317b30a481824f"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "9999",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "http://192.168.1.120:9999",
        "allowMethods": "GET,POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}
4
  • same problem, hadn't it before. did you find a solution? Dec 4, 2018 at 18:24
  • Nope, used a public channel @Shadrix Dec 6, 2018 at 3:16
  • as of today, still not a solution? @AravindSrinivas I just stumbled upon this issue as well. I'm a total noob in this topic, what would be the cons of using Channel instead of PrivateChannel in this case? May 15, 2019 at 1:15
  • I had to move my socket.io-client from version 3.* to 2.2.0. For me that was the problem the entire time. Had nothing to do with Laravel or laravel-echo-server Feb 27, 2021 at 17:50

4 Answers 4

5

did you forgot to uncomment BroadcastServiceProvider::class in config/app.php

App\Providers\BroadcastServiceProvider::class
2

The one thing that sticks that may be problematic is the event namespace you're listening on. The docs show using dot notation as opposed to backslashes:

Echo.private('basket_details.1')
  // change this to '.App.Events.UpdateWebOrderDetailsToBasket' 
  // or just 'UpdateWebOrderDetailsToBasket' since you are using the default App\Events.
  .listen('.App\Events\UpdateWebOrderDetailsToBasket', (e) => {
    console.log("channel started here");
  });

See Namespaces

Can you verify the correct authorization route is being reached? Does any thing print if you log from:

Broadcast::channel('basket_details.{basketId}', function ($user, $basketId) {
    logger('Basked ID: ' . $basketId);
    return true;
});

If you are protecting these routes using the api middleware, for example, you'll need to create the Echo instance with the proper headers:

const client = new Echo({
  auth: {
    headers: {
      Authorization: `Bearer ${token}`
    }
  }
})
2
  • Logger not logging, I' m not using any api's Jul 27, 2018 at 9:11
  • 1
    The NotFoundHttpException might reveal the issue, try logging the exception from app/Exceptions/Handler.php to see if a more informative message or stack trace prints.
    – Brian Lee
    Jul 27, 2018 at 9:51
0

Add <meta name="csrf-token" content="{{ csrf_token() }}"> to your HTML / Layout and that should fix your problem in must cases.

0

In the first time I created, I try alot of things, but it not work, so after that, I turn off all of services,

laravel, nextjs, laravel-echo-server.

And restart -> it work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.