0
votes

I'm new to using laravel pusher I've followed the documentation and watch some tutorials on how to use it but somehow I cant get it work. I'm using laravel 5.6 on, php7.1 and nginx.

I've added all the pusher keys in my .env file already, below are some of my codes

.env

BROADCAST_DRIVER=pusher

web.php

use App\Events\TestEvent;

Route::get('/test', function() {
    event( new TestEvent() );
});

broadcasting.php

'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'),
        'encrypted' => true,
    ],
],

TestEvent.php

<?php

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 TestEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;
    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->user = ['name' => 'Test User', 'email' => '[email protected]'];
        $this->message = 'Hello world';
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('TestChannel');
    }
}

When I tried to access the route /test on my app I expected that Pusher debug console logs anything but it doesn't, even Pusher error log.

enter image description here

Any help would be much appreciated. Thanks!!

3
Maybe check your Laravel logs for any error messages. - Fjarlaegur
@Fjarlaegur it doesnt log any in laravel logs - PenAndPapers
Import this use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; - tayyab_fareed
and than do thisclass TestEvent implements ShouldBroadcastNow - tayyab_fareed
I recommend to not use the Sync driver in production tough. The solution suggested by @tayyab_fareed is useful for debugging, but doing this will execute the broadcast on the main thread instead of using background processes (so it will slow your complete request down). Read this - Fjarlaegur

3 Answers

0
votes

I don't have any experience with Pusher, so please correct me if i am wrong. But according to the documentation, you should fire Events implementing the ShouldBroadcast interface like this:

event(new TestEvent());

Care to explain why your event is using those traits? I dont see Dispatchable referenced anywhere. InteractsWithSockets is only stated as a use statement, not as trait. And as far as i can tell SerializesModels is only needed when trying to send a whole model as data in the event.

0
votes

setting up 'encrypted' => true, to 'encrypted' => false, in broadcasting.php works for me.

0
votes

Its not much clear from your code but you can have a look at routes/channels.php file.

Above file should have below mentioned code

Broadcast::channel('TestChannel', function () {
    # true can be replaced with condition in non-testing environment
    return true; 
});

In addition to this, you can also try replacing

ShouldBroadcast with ShouldBroadcastNow to obtain

class TestEvent implements ShouldBroadcastNow

Also add, use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; under use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

ShouldBroadcastNow skips the queue and broadcasts your event message asap.