vendredi 5 avril 2019

Cancel broadcast from within event

So, in my app, when inviting a new user, I call my UserInvited event like so:

event(new UserInvited($invitation));

Now, this sends an email to the nominated address AND broadcasts an event on a Pusher channel (if the user is already existing):

public function broadcastOn()
{
    $user = User::where('email', $this->invitation->recipient_email)->first();

    if($user){
        return new PresenceChannel('user_'.$user->id);
    }
}

however, if it doesn't exist (and nothing is returned from broadcastOn), it attempts to broadcast to an empty channel, resulting in tons of failed jobs saying 'Invalid channel name'.

Seeing I'd like the event to still be called, but only to actually broadcast if there's an existing user, is there a way to cancel the broadcast from within the event definition? Or should I just return a random string for the channel, and let it broadcast there?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire