javascriptphppusherlaravel-5.3

what event-name should be used on broadcasting laravel notifications


I want to make real-time laravel notifications.

For that I done all configurations described in docs.

My notification class is:

class NewChangeRegisterStatusNotif extends Notification
{
    use Queueable;


    public function __construct ($status_id, $course_id)
    {
        $this->status = CourseUserStatus::findOrFail($status_id);
        $this->course = Course::findOrFail($course_id)->first(['title']);
    }


    public function via ($notifiable)
    {
        return ['database', 'broadcast'];
    }


    public function toMail ($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', 'https://laravel.com')
            ->line('Thank you for using our application!');
    }


    public function toArray ($notifiable)
    {
        return [
            'message' =>
                'Some Messages'
            ,
            'action'  => '#'
        ];
    }
}

And Below codes, create a new Notification :

$user = User::findOrFail($user_id);
$user->notify(new NewChangeRegisterStatusNotif($value, $course_id));

and this is javascript client codes to bind and receive pusher events:

<script src="{{asset('link/to/pusher.min.js')}}"></script>
        <script>
            Pusher.log  = function (msg) {
                console.log(msg);
            };
            var pusher  = new Pusher("{{env("PUSHER_KEY")}}");
            var channel = pusher.subscribe('App.User.1');
                 channel.bind('Illuminate\Notifications\Events\BroadcastNotificationCreated', function (data) {
                console.log(data);
            });
        </script>

subscribing to App.User.1 is succeeded in Debug Console Pusher.

Also in Laravel logs below message is shown :

[11:08:31] LOG.info: Broadcasting [Illuminate\Notifications\Events\BroadcastNotificationCreated] on channels [private-App.User.1] with payload:
{
    "message": "Some Message",
    "action": "#",
    "id": "57d9e4dd-09cd-4cd2-ba3d-06f3fcf2af84",
    "type": "App\\Notifications\\NewChangeRegisterStatusNotif",
    "socket": null
}

But Problem is that channel.bind() does not fired.

As you Can see , I pass Illuminate\Notifications\Events\BroadcastNotificationCreated to that and try escaped it Illuminate\\Notifications\\Events\\BroadcastNotificationCreated but None of them does not work.

What is Problem?

Update:

I found that Despite the Broadcasting event by laravel, but it does not sent to pusher or Pusher can not receive it.


Solution

  • By doing below actions my problem is solved :

    1- in the config/broadcasting.php file on line 18 , I change 'default' => env('BROADCAST_DRIVER', 'pusher'), to 'default' => 'pusher',.

    2- changed var channel = pusher.subscribe('App.User.1'); to var channel = pusher.subscribe('private-App.User.{{$AuthUser->user_id}}');

    3- escaped channel.bind('Illuminate\Notifications\Events\BroadcastNotificationCreated' to channel.bind('Illuminate\\Notifications\\Events\\BroadcastNotificationCreated'

    Also I followed all instructions that described at https://stackoverflow.com/a/30396900/498504