VOOZH about

URL: https://deepwiki.com/hypervel/notifications/5.2-broadcast-channel

⇱ Broadcast Channel | hypervel/notifications | DeepWiki


Loading...
Menu

Broadcast Channel

Overview

The Broadcast Channel delivers notifications as real-time events to connected clients through the broadcasting system. Unlike other channels that directly interact with external APIs, BroadcastChannel wraps notification data in a BroadcastNotificationCreated event that implements ShouldBroadcast, delegating actual delivery to the hypervel/broadcasting package.

Related Pages: See Notification Channels for channel overview, Mail Channel for email delivery, and Slack Channels for Slack integration.

Sources: src/Channels/BroadcastChannel.php13-61 src/Events/BroadcastNotificationCreated.php16-109

Architecture Components

Component Structure

The Broadcast Channel consists of three cooperating classes:

Class Relationships


Sources: src/Channels/BroadcastChannel.php13-21 src/Messages/BroadcastMessage.php9-32 src/Events/BroadcastNotificationCreated.php16-33

BroadcastChannel

Located at src/Channels/BroadcastChannel.php13-61 this channel acts as an event dispatcher rather than a direct delivery mechanism.

MethodResponsibility
send(mixed $notifiable, Notification $notification)Extracts notification data and dispatches event
getData(mixed $notifiable, Notification $notification)Calls toBroadcast() or toArray() on notification

The send() method creates a BroadcastNotificationCreated event and dispatches it through PSR-14 EventDispatcherInterface. If the notification returns a BroadcastMessage, the event inherits its queue configuration via onConnection() and onQueue().

Sources: src/Channels/BroadcastChannel.php26-42 src/Channels/BroadcastChannel.php49-60

BroadcastMessage

Located at src/Messages/BroadcastMessage.php9-32 this class encapsulates broadcast notification data with queue configuration.

PropertyTypePurpose
dataarrayNotification payload to broadcast
Uses QueueabletraitProvides connection, queue, delay properties

The data() method provides a fluent interface for setting payload data. Queue properties (connection, queue) are transferred to the event for asynchronous broadcasting.

Sources: src/Messages/BroadcastMessage.php18-31

BroadcastNotificationCreated

Located at src/Events/BroadcastNotificationCreated.php16-109 this event implements ShouldBroadcast from hypervel/broadcasting and contains the actual broadcast logic.

MethodReturn TypePurpose
broadcastOn()arrayDetermines broadcast channels (WebSocket/Pusher/etc.)
broadcastWith()arrayFormats data payload for clients
broadcastType()stringReturns notification type identifier
broadcastAs()stringCustomizes event name
channelName()array|stringGenerates default channel name from notifiable

The event uses Queueable and SerializesModels traits, allowing asynchronous broadcasting through the queue system.

Sources: src/Events/BroadcastNotificationCreated.php28-108

Delivery Flow

End-to-End Broadcast Notification Processing


Sources: src/Channels/BroadcastChannel.php26-42 src/Channels/BroadcastChannel.php49-60 src/Events/BroadcastNotificationCreated.php38-88

Channel Resolution Logic

The BroadcastNotificationCreated::broadcastOn() method determines broadcast channels through a resolution hierarchy:

Channel Resolution Algorithm


Sources: src/Events/BroadcastNotificationCreated.php38-59 src/Events/BroadcastNotificationCreated.php64-73

Resolution Priority Table

PrioritySourceMethod/PropertyDefault Behavior
1AnonymousNotifiablerouteNotificationFor('broadcast')Returns explicit channels
2NotificationbroadcastOn()Returns notification-defined channels
3NotifiablereceivesBroadcastNotificationsOn($notification)Returns custom channel name
4DefaultchannelName(){ClassName}.{id} format

All resolved channels are wrapped in PrivateChannel instances from hypervel/broadcasting.

Sources: src/Events/BroadcastNotificationCreated.php38-73

Data Extraction and Formatting

Notification Data Extraction

The BroadcastChannel::getData() method at src/Channels/BroadcastChannel.php49-60 follows a fallback hierarchy:


Sources: src/Channels/BroadcastChannel.php49-60

Broadcast Payload Formatting

The BroadcastNotificationCreated::broadcastWith() method at src/Events/BroadcastNotificationCreated.php78-88 constructs the client payload.

Default Payload Structure

FieldSourceTypeDescription
id$notification->idstringNotification UUID
typebroadcastType()stringNotification class name or custom type
...$datamixedNotification-specific data

Customization Methods

Notification MethodOverride Behavior
broadcastWith()Replace entire payload
broadcastType()Replace type field value
broadcastAs()Change event name for clients

Sources: src/Events/BroadcastNotificationCreated.php78-108

Queue Configuration

When notifications return a BroadcastMessage from toBroadcast(), the event inherits its queue configuration:

Queue Property Transfer


Sources: src/Channels/BroadcastChannel.php36-39

The BroadcastMessage uses the Queueable trait from hypervel/bus, providing:

PropertyTypePurpose
connectionstring|nullQueue connection name
queuestring|nullQueue name
delayDateTimeInterface|DateInterval|int|nullBroadcasting delay

These properties are applied to the BroadcastNotificationCreated event at src/Channels/BroadcastChannel.php36-39 allowing asynchronous broadcasting through the queue system.

Sources: src/Messages/BroadcastMessage.php11 src/Channels/BroadcastChannel.php36-39

Broadcasting System Integration

The Broadcast Channel integrates with Hypervel's broadcasting system through the ShouldBroadcast interface.

Key integration points:

  1. The BroadcastNotificationCreated event implements ShouldBroadcast
  2. The event provides the required methods:
    • broadcastOn(): Determines the channels to broadcast on
    • broadcastWith(): Specifies the data to broadcast
    • broadcastAs(): Customizes the event name

Sources: src/Events/BroadcastNotificationCreated.php16-109

Example Usage Pattern

A typical implementation pattern for broadcast notifications:


The Broadcast Channel is particularly useful for real-time applications that need to notify users of events as they happen, such as new messages, status updates, or system alerts.

Sources: src/Channels/BroadcastChannel.php13-61 src/Events/BroadcastNotificationCreated.php16-109