Broadcasting not making it to the front end.
I'm working on a site and wanted to make a page get alerted when a record is added. So I started following the Laravel Docs on Broadcasting with Pusher but I hit a snag. I'm not seeing the Broadcast event when I use listen on that page. There are no errors in the javascript console or in the Laravel logs. I do see connections in the pusher debug, so I think that part is working.
I'm pretty new Laravel so this is probably something minor I forgot or messed up somewhere, any help would be appreciated. Here's my code:
App/Events/NewMessage.php
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewMessage implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new Channel('messageLog'),
];
}
}
In my controller, on the store() function I have:
broadcast(new NewMessage($att['user_id'],$att['message_id']));
resources/js/echo.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,
cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
forceTLS: true
});
On the blade template, I have:
<script type="module">
Echo.channel('messageLog').listen('NewMessage', (e) => {
alert('Gottem');
});
</script>
I'm not sure if anyone would need to see anything else, but let me know and I can add it.
Please or to participate in this conversation.