Try to create sample Laravel 11 project to test SSE with Laravel Wave:
composer create-project laravel/laravel laravel-sse
cd laravel-sse
composer require qruto/laravel-wave
npm install --save-dev laravel-echo laravel-wave sweetalert2
npm run build
Then create event:
php artisan make:event RealTimeNotification
Change event source appEventsRealTimeNotification.php
to:
class RealTimeNotification implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(
public string $message
)
{
}
public function broadcastOn(): array
{
return [
new Channel('events'),
];
}
}
Modify routes (web.php) to:
// Bad: Route::view('/wave', 'welcome');
Route::view('real-time-notifications', 'sse');
I don’t know why these parameters are the following – I got them from Laravel SSE Sample
Then add JavaScript subscriber in resources/js/bootstrap.js
:
import { WaveConnector } from 'laravel-wave';
window.Echo = new Echo({
broadcaster: WaveConnector,
});
window.Echo.channel('events')
.listen('RealTimeNotification', (e) => {
console.log('e', e)
Swal.fire({
position: 'top',
icon: 'success',
title: e.message,
showConfirmButton: false,
});
});
Start Laravel as php artisan serve
.
What I’ve seen at Laravel Development Server console – /wave
message indefinitely. Guess it’s ok.
Then try to push event in tinker as event(new AppEventsRealTimeNotification("Hello World!"));
– no visible reaction at browser console.
What I’ve seen at browser console – nothing. No errors, no valuable data.
What’s wrong?
4