how to send an event in laravel 5.5 using the pusher.js library?

0

I am trying to make notifications in real time with laravel and pusher.js the problem is that when I create the event it does not reach the client but it does not mark any error code:

now edit the serviceProvider, broadCasting, app.php, .env files as well as install the pusher with compuser and npm ¡

some solution I appreciate them? ¡

I would appreciate your help here are the screenshots

    
asked by sami yahir Arevalo Montes 09.06.2018 в 20:53
source

1 answer

0

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>
    
answered by 12.06.2018 / 12:58
source