Events
Wirechat broadcasts events when messages are created and when participants need a private notification.
MessageCreated Event
The MessageCreated event is triggered when a new message is created. It is broadcast over the private channel private-{panel}.conversation.{id}, where {panel} is the panel ID and {id} is the conversation key.
Only conversation participants can subscribe to that private channel.
Listen to the event with Laravel Echo.
Example: Broadcasting the Event
$message = $auth->sendMessageTo($otherUser);
broadcast(new MessageCreated($message));
Broadcasting to a Panel
Without an explicit panel, Wirechat broadcasts through the default panel. Pass a panel ID when the message belongs to another panel:
$message = $auth->sendMessageTo($otherUser);
broadcast(new MessageCreated(message: $message, panel: 'admin'));
NotifyParticipant Event
Some participants may not be actively viewing a conversation but still need to be notified about new messages. The NotifyParticipant event is broadcast over the private channel private-{panel}.participant.{type}.{id} to notify each participant individually.
The {type} and {id} in the channel represent the participant's model type and identifier. This distinction is necessary because Wirechat supports participants from different models. The participant is validated by ensuring that {type} matches the authenticated user’s morphClass and {id} matches their model key. This ensures notifications are sent to the correct participants.
Broadcasting the Event
$message = $auth->sendMessageTo($otherUser);
broadcast(new NotifyParticipant($otherUser, $message));
Broadcasting via Panel
$message = $auth->sendMessageTo($otherUser);
broadcast(new NotifyParticipant(participant: $otherUser, message: $message, panel: 'chats'));
Recap of Events
| Event | Description |
|---|---|
Wirechat\Wirechat\Events\MessageCreated |
Broadcasts to private-{panel}.conversation.{id} when a message is sent, updating the conversation in real-time. |
Wirechat\Wirechat\Events\NotifyParticipant |
Broadcasts to private-{panel}.participant.{type}.{id} to notify a specific participant of a new message. |
Listening to Events
Wirechat events can drive custom UI updates, notification channels, and application listeners.
Handle them in JavaScript with Laravel Echo or on the server with Laravel event listeners.
Client-Side (Using Laravel Echo)
Use Laravel Echo to listen for broadcasted events in the browser.
Setting Up Laravel Echo
Before listening for events, install and configure Laravel Echo with the broadcasting driver used by your application.
Example: Listening for NotifyParticipant
Wirechat provides a helper function to standardize morph class serialization:
Wirechat\Wirechat\Helpers\MorphClassResolver::encode().
The helper encodes model types into the alphanumeric format used by private channel names.
// JavaScript
userId = @js(auth()->id());
encodedType = @js(\Wirechat\Wirechat\Helpers\MorphClassResolver::encode(auth()->user()->getMorphClass()));
panel = 'panel-id'; // or set your custom panel
Echo.private(`${panel}.participant.${encodedType}.${userId}`)
.listen('.Wirechat\\Wirechat\\Events\\NotifyParticipant', (e) => {
console.log(e);
});
Handling Events in Your UI
After receiving an event, your frontend can:
- Update the chat UI in real-time.
- Display notifications to users.
Server-Side Listeners
Use Laravel's event system for server-side listeners such as notification jobs or audit logging.
Example: Registering a Listener for MessageCreated
In your AppServiceProvider, map the MessageCreated event to a listener:
public function boot(): void
{
Event::listen(
\Wirechat\Wirechat\Events\MessageCreated::class,
SendEmailNotification::class,
);
}
In this example, SendEmailNotification runs whenever a message is created.