zedling
zedling

Reputation: 637

Laravel broadcast does not send events to pusher

I've set up Laravel to use pusher to send events for Laravel Echo, but the events I fire from artisan console don't reach pusher. The events i fire from the pusher debug console are working fine, so the frontend part is okay. Could the vagrant VM interfere with it?

My .env file

QUEUE_DRIVER=sync
...
PUSHER_KEY=<key>
PUSHER_SECRET=<secret>
PUSHER_APP_ID=<app_id>

My broadcasting.php

'default' => env('BROADCAST_DRIVER', 'pusher'),
...
'connections' => [

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

The event:

class NewMessage implements ShouldBroadcast
{
    public function broadcastOn()
    {
         return [new Channel('chat.'.$this->conversation->id)];
    }
}

The event is fired from an Observer class, which observes a models created event.

public function created(Chat\Message $message)
{
    event(new NewMessage($message));
}

The Observer is registered in the AppServiceProvider

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Message::observe(MessageObserver::class);
    }
}

When I run the console command which creates a new message the terminal shows this:

php artisan chat:broadcast asdasd
[2016-12-15 16:26:44] local.INFO: Broadcasting [eventname] on channels [channel] with payload: {...}  

Note: most of the names are confidential so I've used placeholders like eventname, channel etc, but I hope the relevant info is there.

Thank you!

Upvotes: 6

Views: 5871

Answers (2)

Bakhtiar
Bakhtiar

Reputation: 679

Before broadcasting any events, you will first need to register the App\Providers\BroadcastServiceProvider. In fresh Laravel applications, you only need to uncomment this provider in the providers array of your config/app.php configuration file.

     /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    //App\Providers\BroadcastServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,

.env file

BROADCAST_DRIVER=pusher

PUSHER_APP_ID=app_id
PUSHER_APP_KEY=auth-key
PUSHER_APP_SECRET=secret
PUSHER_APP_CLUSTER=cluster

you can fire event in controller like this

broadcast(new NewMessage($message));

and you can run php artisan config:cache to ensure

Upvotes: 3

Darshan Jadiye
Darshan Jadiye

Reputation: 241

Make sure that BROADCAST_DRIVER in .env file is set to pusher like this

BROADCAST_DRIVER=pusher

and in broadcasting.php modify the potions array like following code

'pusher' => [
    'driver'  => 'pusher',
    'key'     => env('PUSHER_KEY'),
    'secret'  => env('PUSHER_SECRET'),
    'app_id'  => env('PUSHER_APP_ID'),
    'options' => [
      //leave these empty in your code
    ],
],`

Save this changes and run your laravel server i hope this will work for you ...!!

Upvotes: 9

Related Questions