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

coolpraz's avatar

Event got fired twice

In my app i want to make notification realtime which Laravel Spark don't ship in default installation. Whenever a notification created Laravel Spark fire NotificationCreated event.

Laravel\Spark\Events\NotificationCreated

i have setup listener in my app to listen to this event and fire my own custom event

public function handle(NotificationCreated $event)
{
    dump('done');
    event(new AbcNotificationCreated($event));

    return false;
}

This is my AbcNotification

<?php

namespace App\Events;

use App\Task;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class AbcNotificationCreated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $notification;

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

        $this->dontBroadcastToCurrentUser();
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('notification');
    }
}

By default Spark don't Broadcast NotificationCreated event on any channel so i have to create my own custom event which broadcast notification for frontend to listen and react in realtime.

But the problem is AbcNotificationCreated event got fired twice. PS: Any help or suggestion appreciated

0 likes
3 replies
stephenloky's avatar

When you call "Handle" it will create again the event() I think Maybe you should remove "event(new AbcNotificationCreated($event));" and test it out.

coolpraz's avatar

@STEPHENLOKY - When i remove Listener event only get triggered once but the problem is i need to listen to the event that created by the core of Laravel Spark and on Listener handle method i need to fire another event which broadcast on secure channel that frontend (Vue and Laravel Echo) need to listen which update DOM in realtime. As i said before Core event of laravel only fire event not broadcast it on any channel.

Please or to participate in this conversation.