First fill your .env with the values
BROADCAST_DRIVER=pusher
then the config / broadcasting.php (the KEY I did not put them, also they are usually put in the 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
],
],
In the controller you have to send the event like this
event(new LanzarEvent($datos));
then launch the command 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');
}
}
and to read it in the view
<script src="{{ asset('js/pusher.js') }}"></script>
<script>
Echo.private('lectura')
.listen('LanzarEvent', (e) => {
// Tu metodo
});
</script>