Laravel broadcast does not send events to pusher
Asked Answered
M

2

6

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!

Miracle answered 15/12, 2016 at 16:31 Comment(1)
I'm having the same problem in Laravel 5.1. Funny thing is that it was working and then mysteriously stopped. Haven't figured out why yet. I can tell you that it's not the vagrant VM that's interfering... we had it working on the VM.Plane
B
9

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 ...!!

Berky answered 19/1, 2017 at 11:46 Comment(1)
Seems you can help me. Look at this : #45878337Campanulaceous
G
3

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

Giulietta answered 5/3, 2019 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.