I am using Laravel Echo server, redis and socket.io for Chat Messages.
I am broadcasting event using.
event(new MessageSent($messageThread));
This is private channel with method brodcastOn
as follows:
public function broadcastOn()
{
return new PrivateChannel('message.' . $this->broadcastUser->id);
}
My routes/channels.php
looks like:
Broadcast::channel('message.*', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Below is my laravel-echo-server.json
:
{
"authHost": "http://localhost:8000",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "******",
"key": "*************"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port":"6379",
"host":"127.0.0.1",
"db":"0",
"password":"****"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
}
jQuery for listening to events:
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
client: socketio,
auth: {headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr("content"),
}},
});
var userId = window.APP.user;
const channel = window.Echo.channel(`app_database_private-message.${userId}`);
channel.listen('MessageSent', (payload) => {
console.log('payload', payload)
});
this my redis section from config/database.php
:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
'prefix' => Str::slug(env('APP_NAME', 'laravel'), '_').'_database_',
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
It is showing below error:
L A R A V E L E C H O S E R V E R
version 1.6.0
⚠ Starting server in DEV mode...
✔ Running at localhost on port 6001
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
[1:21:22 PM] - Preparing authentication request to: http://localhost:8000
[1:21:22 PM] - Sending auth request to: http://localhost:8000/broadcasting/auth
⚠ [1:21:22 PM] - IQ7eT9P_RzF62lRSAAAA could not be authenticated to app_database_private-message.3
{ "message": "", "exception": "Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException", "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php", "line": 81, "trace": [ { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php", "line": 58, "function": "verifyUserCanAccessChannel", "class": "Illuminate\Broadcasting\Broadcasters\Broadcaster", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php", "line": 319, "function": "auth", "class": "Illuminate\Broadcasting\Broadcasters\RedisBroadcaster", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php", "line": 237, "function": "__call", "class": "Illuminate\Broadcasting\BroadcastManager", "type": "->" }, { "file": "/opt/edugem/apps/project/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": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php", "line": 54, "function": "call_user_func_array" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php", "line": 45, "function": "callAction", "class": "Illuminate\Routing\Controller", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 219, "function": "dispatch", "class": "Illuminate\Routing\ControllerDispatcher", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 176, "function": "runController", "class": "Illuminate\Routing\Route", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 680, "function": "run", "class": "Illuminate\Routing\Route", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 30, "function": "Illuminate\Routing\{closure}", "class": "Illuminate\Routing\Router", "type": "->" }, { "file": "/opt/edugem/apps/project/app/Http/Middleware/AppMiddleware.php", "line": 55, "function": "Illuminate\Routing\{closure}", "class": "Illuminate\Routing\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "App\Http\Middleware\AppMiddleware", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\Pipeline\{closure}", "class": "Illuminate\Pipeline\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php", "line": 41, "function": "Illuminate\Routing\{closure}", "class": "Illuminate\Routing\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\Routing\Middleware\SubstituteBindings", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\Pipeline\{closure}", "class": "Illuminate\Pipeline\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php", "line": 75, "function": "Illuminate\Routing\{closure}", "class": "Illuminate\Routing\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\Foundation\Http\Middleware\VerifyCsrfToken", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\Pipeline\{closure}", "class": "Illuminate\Pipeline\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php", "line": 49, "function": "Illuminate\Routing\{closure}"
2
Answers
You have 2 issues:
You also must ensure that the Laravel config variable
database.redis.options.prefix
is set to''
.https://laravel.com/docs/7.x/broadcasting#defining-authorization-callbacks
instead of
use this statement
use dot(.) before namespace in Echo
example
and reWrite
broadcastAs
in Notification Class and useshouldBroadCastNow
: