简体   繁体   中英

Laravel 5.3 not sending Events to pusher

I'm using laravel 5.3 for my website. I needed to add real time functionality to my app so I used pusher. but the problem is when the event has been triggered nothing happened and no events sent to pusher.

my pusher configuration in broadcasting.php file :

 'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_KEY'),
        'secret' => env('PUSHER_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => 'eu',
            'encrypted'=>true
        ],
    ],

my event class:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

 class ChatEvent implements ShouldBroadcast
  {
   use InteractsWithSockets, SerializesModels;

public $data;

/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct($data)
{
    $this->data = $data;

}

/**
 * Get the channels the event should broadcast on.
 *
 * @return Channel|array
 */
public function broadcastOn()
{

    // return new PrivateChannel('test-channel');
    return ['test-channel'];
}

and my pusher javascript code:

 Pusher.logToConsole = true;

var pusher = new Pusher('pusher_id', {
  cluster:'eu',
  encrypted: true
});

var channel = pusher.subscribe('test-channel');
channel.bind('App\\Events\\ChatEvent', function(data) {
    console.log(data);
  alert(data);
});

Check your Pusher credentials in your .env file.

PUSHER_KEY should be PUSHER_APP_KEY
PUSHER_SECRET should be PUSHER_APP_SECRET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM