I'm trying to learn laravel broadcasting and laravel echo. I'm completely new in this subject. So, I've been following some tutorials and documentations to hopefully learn this concept. But, I've been struggling for hours now and I guess its time to ask for some help. I'm currently pairing laravel echo with laravel websockets.
What I wanted to do was output a simple console.log() by listening to an event using laravel echo. I've been following the guide from the official laravel websockets documentation.
- Install laravel websockets via composer.
- Publish config and migration files.
- migrate the database.
- Install pusher via composer.
- Configure the .env of my project.
- Configure the config/broadcasting.php
- Configure the config/websockets.php
- Configure the js/bootstrap.js (there was already code for laravel echo so I just uncommented it.)
- I created an event named OrderPlaced just to test the code. Then configured it
- I uncommented the BroadcastServiceProvider on config/app.php
- I ran
php artisan serve, npm run dev, php artisan websockets:serve
.env file
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=broadcasting
PUSHER_APP_KEY=broadcasting
PUSHER_APP_SECRET=broadcasting
PUSHER_HOST=127.0.0.1
PUSHER_PORT=6001
PUSHER_SCHEME=http
PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
config/broadcasting.php (I just copied the code from laravel-websockets documentation)
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'http'
],
],
config/websockets.php
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'enable_client_messages' => false,
'enable_statistics' => true,
],
],
resources/js/bootstrap.js
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_PUSHER_APP_KEY,
wsHost: import.meta.env.VITE_PUSHER_HOST ?? `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});
resources/js/app.js
import './bootstrap';
welcome.blade.php
<!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">
@vite('resources/js/app.js')
<title>Document</title>
</head>
<body>
<script>
Echo.private(`orders.${orderId}`)
.listen('OrderPlaced', (e) => {
console.log(e.order);
});
</script>
</body>
</html>
routes/web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('event', function () {
$order = \App\Models\Order::create();
\App\Events\OrderPlaced::dispatch($order);
});
After setting it all up, when I go to my welcome page and check the console its giving me an "Uncaught ReferenceError: Echo is not defined" error.