← BACK TO BLOG
Laravel Events

Instantly Broadcast Real-Time Database Notifications Using Model Observers

Punyapal Shah • • 1 min read
edit on GitHub →

Automatically broadcast WebSocket events to private user channels whenever a database notification is saved using an observer on Laravel's DatabaseNotification model.

Laravel's notification system supports both database persistence (via: ['database']) and real-time broadcasting (via: ['broadcast']). However, configuring notifications to broadcast simultaneously often duplicates payload formatting and requires configuring separate broadcast channels for each notification class.

By attaching an observer to Laravel's underlying DatabaseNotification model, you can automatically broadcast every database notification in real time over private channels with zero per-notification boilerplate.

1. Register the Observer in AppServiceProvider

namespace App\Providers;

use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        DatabaseNotification::observe(new class {
            public function created(DatabaseNotification $notification): void
            {
                broadcast(new class($notification) {
                    public function __construct(
                        public DatabaseNotification $notification
                    ) {}

                    public function broadcastOn(): PrivateChannel
                    {
                        // Derives channel from notifiable type (e.g. 'users.42')
                        $type = strtolower(class_basename(str($this->notification->notifiable_type)->plural()));
                        return new PrivateChannel("{$type}.{$this->notification->notifiable_id}");
                    }

                    public function broadcastAs(): string
                    {
                        return 'DatabaseNotificationCreated';
                    }
                });
            }
        });
    }
}

2. Listening with Laravel Echo (Frontend)

In your client-side JavaScript or Alpine.js component, subscribe to the user's private channel:

Echo.private(`users.${userId}`)
    .listen('.DatabaseNotificationCreated', (e) => {
        console.log('New notification received:', e.notification);
    });

3. Listening in Livewire Components

In Livewire 3 components, use the #[On] attribute:

namespace App\Livewire;

use Livewire\Attributes\On;
use Livewire\Component;

class NotificationBell extends Component
{
    #[On('echo-private:users.{userId},.DatabaseNotificationCreated')]
    public function onNotificationReceived(array $payload): void
    {
        $this->dispatch('refreshNotifications');
    }
}

Summary

  • Observe DatabaseNotification::created to automatically broadcast real-time events for every stored database notification.
  • Eliminates the need to duplicate broadcast payload definitions in individual notification classes.
  • Directly integrates with Laravel Echo, Livewire, and Vue/React frontend architectures.

Related Articles

View all articles →
Laravel Testing

Fixing Slow and Failing Laravel Pest Parallel Tests on Windows

Parallel Pest runs on Windows often fail with WIN1252 encoding errors or out of shared memory lock crashes. Here is how to configure PostgreSQL, OpenSSL, and your build pipeline for fast, reliable parallel testing.

READ >

// Got thoughts or a topic request? Discussions and feedback welcome