Lo primero rellenar tu .env con los valores
BROADCAST_DRIVER=pusher
luego el config/broadcasting.php (las KEY no las puse, ademas se suelen poner en el ENV)
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => 'eu',
'encrypted' => true
],
],
En el controlador tienes que enviar el evento asi
event(new LanzarEvent($datos));
despues lanzar el comando php artisan make:event
<?php
namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class LanzarEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $datos;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($datos)
{
$this->datos= $datos;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('lectura');
}
}
y para leerlo en la vista
<script src="{{ asset('js/pusher.js') }}"></script>
<script>
Echo.private('lectura')
.listen('LanzarEvent', (e) => {
// Tu metodo
});
</script>