I’m trying to broadcast notifications via a private channel to my web application for users who are logged in. Here’s what I’ve done so far:
I set up Laravel Echo and Pusher.
When I test with php artisan tinker, I can see data in the debug console of Pusher-JS.
I checked Telescope, and the status for [post] /broadcasting/auth is 200.
However, despite all this, the frontend doesn’t display the notifications. Any suggestions on how to troubleshoot this issue would be greatly appreciated!
Thank you so much~
I ensure that AppProvidersBroadcastServiceProvider::class is not commented.
(Laravel 11) .env:
BROADCAST_CONNECTION=pusher
AlertMenu.tsx:
window.Pusher = Pusher;
const echo = new Echo({
broadcaster: 'pusher',
key: "xxxxxxxxxxxxxxx",
cluster: "mt1",
forceTLS: true,
});
BroadcastServiceProvider.php
<?php
namespace AppProviders;
use IlluminateSupportFacadesBroadcast;
use IlluminateSupportServiceProvider;
class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Broadcast::routes();
require base_path('routes/channels.php');
}
}
channels.php
<?php
use IlluminateSupportFacadesBroadcast;
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('project.{project_id}.alerts', function ($user, $project_id) {
if($user->current_project_id == $project_id){
return true;
}else{
return false;
}
});
I have also tried to add the controller and route to the web.php but the status code will change from 200 to 419,
web.php
Route::post('/broadcasting/auth', [PusherController::class, 'pusherAuth'])->middleware('auth');
PusherController:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use PusherPusher;
class PusherController extends Controller
{
public function pusherAuth(Request $request)
{
$user = auth()->user();
$socket_id = $request['socket_id'];
$channel_name = $request['channel_name'];
$key = getenv('PUSHER_APP_KEY');
$secret = getenv('PUSHER_APP_SECRET');
$app_id = getenv('PUSHER_APP_ID');
if ($user) {
$pusher = new Pusher($key, $secret, $app_id);
$auth = $pusher->socket_Auth($channel_name, $socket_id);
return response($auth, 200);
} else {
header('', true, 403);
echo "Forbidden";
return;
}
}
}