Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joseph127's avatar

Laravel 5.3 Notification Broadcasting

I've set it up with the database, and everything is inserted correctly there.

My Notification Class

 <?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class CommentedOnScreen extends Notification implements ShouldBroadcast
{
    use Queueable;

    protected $comment;

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

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }


    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {

        return [
            'from_user' => $this->comment['user_id'],
            'dot_id' => $this->comment['dot_id'],
            'comment' => $this->comment['comment'],
        ];
    }
}

although when this is fired it gets sent to the database but not to pusher. Everything for pusher is setup in my .env file.

'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                //
            ],
        ],
BROADCAST_DRIVER="pusher"
PUSHER_KEY="public_key"
PUSHER_SECRET="secret_key"
PUSHER_APP_ID=app_id

and I am listening correctly with Laravel Echo, and thats being recorded on Pusher, but all my notifications etc are not received by pusher? Can anyone see something I am doing wrong?

0 likes
38 replies
Jaytee's avatar

I've yet to use this myself however have you set the event broadcasting up?

By default, the BroadCastServiceProvider is commented out in configure/app.

Make sure it's uncommented

1 like
joseph127's avatar

Yeah I uncommented the BroadcastServiceProvider and followed the steps on the documentation for setting up broadcasting events. So I am pulling my hair out to the reason the events aren't being pushed to pusher or picked up by the Laravel Echo notification listener?

Jaytee's avatar

Watch the "What's new in Laravel 5.3 on broadcasting" see if that helps

I can't help you anymore as I'm not familiar with this myself however check and see if there are any bugs relating to it

Good luck

joseph127's avatar

I am really hoping he was going to go over how to use the broadcasted notifications, he did Mail, Database and Slack so he should haha! Yeah I'll look out for a broadcasting in laravel 5.3 lesson. Thanks anyway, maybe someone else knows why ????

Jaytee's avatar

Is everything set properly in broadcast.php in config?

Jaytee's avatar

Wait just out of interest, how are you calling your notification?

joseph127's avatar

$user->notify(new CommentedOnScreen($comment));

joseph127's avatar

The database record gets inserted so I assume everything gets called correctly.

Szyfr's avatar

let me see you BroadcastServiceProvider and js code.

joseph127's avatar
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes();

        Broadcast::channel('App.User.*', function ($user, $userId) {
            return (int) $user->id === (int) $userId;
        });
    }
}
Echo.private('App.User.' + vm.user.id).notification((notification) => {
       console.log(notification);
});

Pusher says that it gets a connection on private-App.User.1 so thats working correctly (the listener) its just not receiving anything.

Szyfr's avatar

maybe your broadcast closure, try this first, on your Broadcast provider

Broadcast::channel('App.User.1', function () {
            return true;
});

on js

Echo.private('App.User.1')
            .notification((notification) => {
                console.log(notification);
});
                

then on your chrome console log, check Preserve log

Szyfr's avatar

weird, mine is working, review your js, mine

    ready() {
        this.notify();
    },
methods: {

notify() {
            Echo.private('App.User.1')
            .notification((notification) => {
                console.log(notification);
            });
        },
},
joseph127's avatar

Still seems to not be working even though I have used the exact same code as you. Very strange, is there anything you're doing in your Broadcast provider that I am not doing?

joseph127's avatar

When I update my BROADCAST_DRIVER to log, I also see nothing in the logs?

Szyfr's avatar

did you properly install and import echo in bootstrap.js?

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your-key-here'
});

and try adding this on your EventServiceProvider

'Illuminate\Notifications\Events\NotificationSent' => [
        'App\Listeners\LogNotification',
    ],

then run php artisan event:generate

joseph127's avatar

Yeah @Encrypt the Laravel echo is setup correctly, as when I refresh the page. I can see the connections being made and OCCUPIED by the users on the site. It's just sending of broadcasted notifications. I tried adding that event listener in which works, in the handel function I run.

Log::info($event->notifiable); which logs the notifiable user, so thats clearly being ran. Its the broadcast method itself which is not. As I said the database entry get inserted each time, so not sure why broadcast isn't running?

joseph127's avatar

The log does run twice on each time the comment is made. So I am assuming both 'database' and 'broadcast' events are running, but for some reason broadcast is not sending information to pusher?

Szyfr's avatar

@joseph127 try to add type-hint

<?php

namespace App\Notifications;

use App\Comment;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class CommentedOnScreen extends Notification
{
    use Queueable;

    protected $comment;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Comment $comment)
    {
        $this->comment = $comment;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }


    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {

        return [
            'from_user' => $this->comment['user_id'],
            'dot_id' => $this->comment['dot_id'],
            'comment' => $this->comment['comment'],
        ];
    }
}
joseph127's avatar

@Encrypt I notice you have't implemented ShouldBroadcast. Is this not needed for broadcasting?

Szyfr's avatar

yup, its not needed, implement ShouldBroadcast its already in BroadcastNotificationCreated.php

Szyfr's avatar

I can't help you anymore, here check my sample code, mine is working.

<?php

namespace App\Notifications;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserRegistered extends Notification
{
    use Queueable;

    protected $user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'id' => $this->user->id,
            'name' => $this->user->name,
            'email' => $this->user->email,
            'created_at' => $this->user->created_at,
        ];
    }
}

in app.php uncomment

App\Providers\BroadcastServiceProvider::class,

UsersController

use App\Events\UserHasRegistered;
use App\Notifications\UserRegistered;

public function store(Request $request)
    {
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password)
        ]);

        Auth::user()->notify(new UserRegistered($user));
        event(new UserHasRegistered($user));
        
        return back();
    }

app.js


/**
 * First we will load all of this project's JavaScript dependencies which
 * include Vue and Vue Resource. This gives a great starting point for
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the body of the page. From here, you may begin adding components to
 * the application, or feel free to tweak this setup for your needs.
 */

/*Vue.component('example', require('./components/Example.vue'));*/

var app = new Vue({
    el: 'body',

    data() {
        return {
            users: [],
            notifications: []
        }
    },

    ready() {
        this.listen();

        this.notify();

        this.getUsers();

        this.getNotifications();
    },

    methods: {
        listen() {
            Echo.channel('test')
                .listen('UserHasRegistered', (e) => {
                    this.users.push(e.user);
            });
        },

        notify() {
            Echo.private('App.User.1')
            .notification((notification) => {
                console.log(notification);
                this.notifications.push(notification);
            });
        },

        getUsers() {
            this.$http.get('/api/users')
                .then(response => {
                    this.users = response.data;
            });
        },

        getNotifications() {
            this.$http.get('/api/notifications')
                .then(response => {
                    this.notifications = response.data;
            });
        }
    }
});

broadcastserviceprovider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes();

        /*
         * Authenticate the user's personal channel...
         */
        Broadcast::channel('App.User.*', function ($user, $userId) {
            return (int) $user->id === (int) $userId;
        });
    }
}

and don't forget to login using id 1 in my code

joseph127's avatar

I've literally tried that exact code... And I am totally stoked why this isn't working. Could it be anything to do with the version of laravel5.3 I have, although I have ran composer update and still no broadcasted event, yet the database entry is entered fine :(

Jaytee's avatar

Create a new 5.3 project and try set it up and see if it works. If it does, you may have a corrupt install so port it over

Szyfr's avatar

ryt, try to create again a new 5.3 project.

then install pusher and echo again

composer require pusher/pusher-php-server

npm install --save laravel-echo pusher-js
matbeard's avatar

Do you use Debugbar? I had similar problems and they were caused by the Debugbar output from the BroadcastServiceProvider authentication response. Adding \Debugbar::disable(); fixed my issues:

Broadcast::channel('App.User.*', function ($user, $userId) {
            \Debugbar::disable();
            return (int) $user->id === (int) $userId;
});
joseph127's avatar

@matbeard no I don't, I don't really know whats causing this issue, I think I am going to have to start the project again on another install of laravel5.3 haha!

swoop's avatar

Hi @joseph127

Maybe you did the same mistake as me? Have you checked if you .env file is setup correctly? I made the mistake coping the the example from Pusher into my .env file and forgot to change it back the way Laravel wants it.

So I ended up with this:

PUSHER_APP_ID="1234567"
PUSHER_APP_KEY="my-pusher-key"
PUSHER_APP_SECRET="my-pusher-secret"

instead of the correct:

PUSHER_APP_ID=1234567
PUSHER_KEY="my-pusher-key"
PUSHER_SECRET="my-pusher-secret"

In other words. Make sure it says PUSHER_KEY. - Not PUSHER_APP_KEY. And PUSHER_SECRET. - Not PUSHER_APP_SECRET

Just in case you made the same mistake I did? :)

1 like
homeup's avatar

@joseph127 Have you figured this out yet? Does it work for you if, from within your js file, you use "echo.channel('private-App.User...", instead of "echo.private('App.User..."? I tried this based on one of your early comments on this thread and it ended up working for me. Not sure if this approach is going to end up creating other issues for me down the road, though.

Next

Please or to participate in this conversation.