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

eggplantSword's avatar

Events vs Notifications

I've been trying to incorporate Notifications using pusher but was having trouble seeing the notification on pusher until I tried an event which works and shows up on pusher while none of the Notifications did. Do I have to use an Event along with a Notification? It doesn't seem so in the docs but maybe I missed it.

When should I use one or the other and in which situations are Notifications better than Events?

0 likes
2 replies
LaryAI's avatar
Level 58

Notifications and events serve different purposes in Laravel. Notifications are used to notify users of specific events that occur in the application, such as a new message or a new follower. Events, on the other hand, are used to trigger actions in response to specific events, such as sending an email or updating a database record.

In terms of using Pusher, both notifications and events can be used to trigger real-time updates in the application. However, notifications are typically used for user-facing updates, while events are used for backend updates.

If you are having trouble getting notifications to work with Pusher, it may be worth checking your configuration to ensure that everything is set up correctly. However, if events are working for your use case, there is no need to use notifications as well.

In general, notifications are better suited for user-facing updates, while events are better suited for backend updates. However, the specific use case will depend on the requirements of your application.

Here is an example of using an event to trigger a real-time update with Pusher:

// Define the event
class NewMessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    public function __construct(Message $message)
    {
        $this->message = $message;
    }

    public function broadcastOn()
    {
        return new Channel('messages');
    }
}

// Trigger the event
event(new NewMessageEvent($message));

And here is an example of using a notification to trigger a real-time update with Pusher:

// Define the notification
class NewMessageNotification extends Notification implements ShouldBroadcast
{
    use Queueable;

    public $message;

    public function __construct(Message $message)
    {
        $this->message = $message;
    }

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

    public function toArray($notifiable)
    {
        return [
            'message' => $this->message->body,
        ];
    }

    public function broadcastOn()
    {
        return new Channel('notifications');
    }
}

// Trigger the notification
$user->notify(new NewMessageNotification($message));
eggplantSword's avatar

@LaryAI That notification class is incorrect and does not broadcast an event. After a while I figured out I needed to add

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

Please or to participate in this conversation.