atton53's avatar

How can i broadcast database notification to my app?

I configured laravel reverb and it is doing Ping - Pong perfectly. However the notifications are not broadcasting.

This is the method of my post like controller where i am dispatching the notification

This i my post like notification

This is my channel

Broadcast::channel('user.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

And this is my user model

This is where i am receiving the broadcast

0 likes
1 reply
LaryAI's avatar
Level 58

To broadcast database notifications in Laravel, you need to ensure that several components are correctly configured. Let's go through the necessary steps and check your current setup:

  1. Ensure Broadcasting is Configured:

    • Make sure you have configured the broadcasting service in your config/broadcasting.php file. For example, if you're using Pusher, ensure you have the correct credentials set up.
  2. Check Your Notification Class:

    • Your PostLiked notification class seems correctly set up to broadcast. It uses the via method to specify both database and broadcast channels, and the toBroadcast method is defined.
  3. Ensure the User Model Implements the Correct Interface:

    • Your User model should use the Notifiable trait, which it does. This is necessary for the user to receive notifications.
  4. Check Your JavaScript Setup:

    • Ensure that you have included the Echo library and configured it correctly. You should have something like this in your JavaScript setup:
    import Echo from 'laravel-echo';
    window.Pusher = require('pusher-js');
    
    window.Echo = new Echo({
        broadcaster: 'pusher',
        key: process.env.MIX_PUSHER_APP_KEY,
        cluster: process.env.MIX_PUSHER_APP_CLUSTER,
        encrypted: true
    });
    
  5. Verify Your Channel Authorization:

    • Your channel authorization seems correct. The Broadcast::channel method checks if the authenticated user ID matches the ID in the channel name.
  6. Check Your Event Listener:

    • In your Vue component, ensure that the Echo listener is correctly set up to listen for notifications:
    Echo.private('user.' + user.id)
        .notification((notification) => {
            console.log(notification.type);
            // You can also update your notifications array here
            unreadNotifications.value.push(notification);
        });
    
  7. Ensure Your Server is Running the Queue Worker:

    • If your notifications are queued, make sure you have a queue worker running. You can start a queue worker using:
    php artisan queue:work
    
  8. Check for Errors:

    • Look at your browser's console and Laravel logs for any errors that might indicate why the notifications aren't being broadcasted.

By following these steps, you should be able to broadcast notifications successfully. If you still encounter issues, double-check each configuration step and ensure that your environment variables are correctly set.

Please or to participate in this conversation.