Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel event not send to pusher

i have pusher app, then i try to send message to pusher by laravel event, this is my event :

namespace App\Events;

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

class NewOrder
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $email;
    public $message;


    public function __construct($email)
    {
        $this->email = $email;
        $this->message = "New order from {$email}, please check.";
        //
    }

    public function broadcastOn()
    {
        return ['new-order'];
    }

}

and i try to test it with route test,

Route::get('test', function () {
    event(new App\Events\NewOrder('John'));
    return "Event has been sent!";
});

my pusher configuration have been configured like my pusher's account configuration, but after access /test , the pusher debug console doesn't show anything.

enter image description here this is my broadcasting.php

    'default' => env('BROADCAST_DRIVER', 'pusher'),


    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                // 'cluster' => 'ap1',
                'encrypted' => true,
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

];

i don't know to see pusher's log from laravel app, anyone can help me? i'm using laravel 5.5

like image 779
Mamen Avatar asked May 29 '18 07:05

Mamen


3 Answers

I think the problem is with your event class declaration, you should implement ShouldBroadcastNow as it fires the event immediately and doesn't require queue implementation. By default you need to set up a queue and then run that queue to fire events.

use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class NewOrder implements ShouldBroadcastNow
{
  ....do your stuff....
}

And also checkout this amazing playlist, this will solve your all problems for sure

like image 100
tayyab_fareed Avatar answered Oct 22 '22 09:10

tayyab_fareed


I also had this problem for about 2 hours.

You forgot to implement broadcast,

you can either use:

use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class NewOrder implements ShouldBroadcastNow

or

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewOrder implements ShouldBroadcast

The latter then requires a queue driver

Happy coding :)

like image 4
james ace Avatar answered Oct 22 '22 11:10

james ace


Replace this:

class NewOrder

With this:

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewOrder implements ShouldBroadcast

That should work. Let me know if I can help more.

like image 1
Mario Arteaga Avatar answered Oct 22 '22 11:10

Mario Arteaga