I can’t get Laravel Reverb and Echo to work, and I’m not sure how to debug it.
I ran through the default installation and just tried to get a simple message to pass to a page.
Here’s my Event:
class TestEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public string $message;
public function __construct(string $message)
{
$this->message = $message;
}
public function broadcastOn()
{
return [new Channel('test-channel')];
}
}
Here’s the Artisan command to dispatch it:
public function handle()
{
TestEvent::dispatch('This is a test');
}
This is the view:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Echo?</title>
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body>
<h1>Echo?</h1>
<script>
Echo.channel('test-channel')
.listen('TestEvent', (e) => {
console.log('TestEvent resp...');
console.log(e);
});
</script>
</body>
</html>
The Echo js file hasn’t been touched:
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});
and Reverb added all the .env values, and I haven’t touched those.
So, I figured all I had to do was run npm run dev
, start the Reverb server, trigger the event, and I should see the console.log output. But nothing. Eventually I get a ‘websocket failed to connect’ error.
I’m using Herd for development, but didn’t think that would make any difference, since everything is running through localhost. I even checked to make sure port 8080 wasn’t being used, and nothing there.
Does anyone have any idea what could be wrong? Or how I can debug it?