VOOZH about

URL: https://deepwiki.com/hypervel/broadcasting/2.2-broadcaster-base-class

⇱ Broadcaster Base Class | hypervel/broadcasting | DeepWiki


Loading...
Menu

Broadcaster Base Class

Purpose & Scope

The Broadcaster abstract base class provides the foundational implementation for all broadcasting drivers in the Hypervel Broadcasting package. It handles channel authentication, parameter extraction, implicit model binding, and user retrieval. All concrete broadcaster implementations (Pusher, Ably, Redis, Log, Null) extend this class to inherit these capabilities.

This page focuses on the shared authentication and authorization logic. For information about specific driver implementations, see Broadcasting Drivers. For details about channel types and the HTTP authentication endpoints, see Channel Types & Authentication. For the factory that manages broadcaster instances, see BroadcastManager.

Sources: src/Broadcasters/Broadcaster.php1-304


Class Structure & Dependencies

The Broadcaster abstract class implements the BroadcasterContract interface and provides shared functionality for all broadcaster implementations.

Core Properties

PropertyTypeScopePurpose
$containerContainerInterfaceprotectedDependency injection container for resolving services
$authenticatedUserCallback?ClosureprotectedCustom callback for user authentication (e.g., Pusher user auth)
$channelsarraystatic protectedRegistry of channel patterns and their authorization callbacks
$channelOptionsarraystatic protectedConfiguration options (guards, etc.) for each channel pattern

Sources: src/Broadcasters/Broadcaster.php23-44

Class Relationships


Sources: src/Broadcasters/Broadcaster.php1-23 src/Broadcasters/Broadcaster.php243-262


Channel Registration System

The Broadcaster class maintains a static registry of channel patterns and their associated authorization callbacks. This registry is shared across all broadcaster instances.

Registration Method

The channel() method registers authorization callbacks for specific channel patterns:


Sources: src/Broadcasters/Broadcaster.php70-85

Pattern Syntax

Channel patterns support parameter placeholders using curly brace syntax:

PatternExample ChannelExtracted Parameters
order.{orderId}order.123['orderId' => '123']
user.{userId}.notificationsuser.456.notifications['userId' => '456']
chat.{roomId}.{messageId}chat.abc.xyz['roomId' => 'abc', 'messageId' => 'xyz']

Sources: src/Broadcasters/Broadcaster.php164-170

Storage Structure


Sources: src/Broadcasters/Broadcaster.php38-44 src/Broadcasters/Broadcaster.php80-82


Authentication Flow

The authentication system verifies whether a user can access a specific channel by matching the channel name against registered patterns and executing authorization callbacks.

Complete Authentication Process


Sources: src/Broadcasters/Broadcaster.php88-114

Pattern Matching Logic

The channelNameMatchesPattern() method converts pattern placeholders to regex capture groups:


Sources: src/Broadcasters/Broadcaster.php280-286


Parameter Extraction & Binding

The parameter extraction system extracts values from channel names and prepares them for authorization callbacks, supporting both scalar values and implicit model binding.

Extraction Pipeline


Sources: src/Broadcasters/Broadcaster.php116-128

Parameter Type Resolution

The extractParameters() method supports three callback types:

Callback TypeResolution MethodExample
ClosureReflectionFunctionfunction($user, $orderId) { }
Class stringReflectionClass::getMethod('join')OrderChannel::class
Callable arrayReflectionFunction (fallback)[$object, 'method']

Sources: src/Broadcasters/Broadcaster.php130-160

Channel Key Extraction

The extractChannelKeys() method uses regex to capture parameter values:


Sources: src/Broadcasters/Broadcaster.php162-170


Implicit Model Binding

The broadcaster supports implicit model binding, automatically resolving route parameters to model instances when the callback signature indicates a model type.

Binding Resolution Process


Sources: src/Broadcasters/Broadcaster.php184-215

Binding Requirements

For implicit binding to occur, a callback parameter must satisfy:

  1. Name Match: Parameter name must match the placeholder name
  2. Type Hint: Parameter must be type-hinted with a class
  3. Interface: The class must implement UrlRoutable interface

Sources: src/Broadcasters/Broadcaster.php208-215


User Retrieval & Guard Support

The broadcaster retrieves authenticated users with support for multiple authentication guards, allowing different channels to authenticate against different user sources.

User Retrieval Flow


Sources: src/Broadcasters/Broadcaster.php240-262

Options Resolution

The retrieveChannelOptions() method matches the channel name against registered patterns to retrieve configuration:


Sources: src/Broadcasters/Broadcaster.php264-278

Guard Configuration Example

When registering channels, guards can be specified in the options array:

Channel RegistrationGuard Behavior
channel('user.{id}', callback)Uses default guard
channel('user.{id}', callback, ['guards' => 'api'])Tries only api guard
channel('user.{id}', callback, ['guards' => ['api', 'web']])Tries api, then web

Sources: src/Broadcasters/Broadcaster.php245-259


Callback Normalization

The broadcaster supports multiple callback formats and normalizes them to a consistent callable format.

Callback Type Support


Sources: src/Broadcasters/Broadcaster.php227-238

Class-Based Channel Handler

When a class string is provided, the broadcaster:

  1. Resolves the class from the container via $this->container->get($callback)
  2. Calls the join() method on the resolved instance
  3. Passes all parameters to the join() method

This allows for class-based authorization handlers with dependency injection:

OrderChannel::class
 ↓ (resolved from container)
OrderChannel instance
 ↓ (method invocation)
$instance->join($user, $order)

Sources: src/Broadcasters/Broadcaster.php234-237


Authenticated User Callback

For services like Pusher that require user authentication (separate from channel authentication), the broadcaster supports custom user resolution callbacks.

User Authentication API

MethodParametersPurpose
resolveAuthenticatedUserUsing()Closure $callbackRegister custom user resolution callback
resolveAuthenticatedUser()RequestInterface $requestExecute registered callback or return null

Sources: src/Broadcasters/Broadcaster.php46-67

Usage Flow


Sources: src/Broadcasters/Broadcaster.php50-57 src/Broadcasters/Broadcaster.php59-67


Utility Methods

The broadcaster provides several utility methods for channel management and inspection.

Channel Management API

MethodReturn TypePurpose
getChannels()CollectionReturns all registered channel patterns as a collection
flushChannels()voidClears both $channels and $channelOptions static arrays
formatChannels()arrayConverts channel objects to string array representation

Sources: src/Broadcasters/Broadcaster.php288-304 src/Broadcasters/Broadcaster.php217-225

Format Channels Example

The formatChannels() method ensures all channel identifiers are strings:


Sources: src/Broadcasters/Broadcaster.php217-225


Integration Points

The Broadcaster base class integrates with several other system components:

Dependency Summary


Sources: src/Broadcasters/Broadcaster.php6-21


Abstract Methods

The Broadcaster class is abstract and requires concrete implementations to provide driver-specific functionality. Subclasses must implement the following methods from BroadcasterContract:

  • auth(RequestInterface $request) - Handle channel authorization requests
  • validAuthenticationResponse(RequestInterface $request, mixed $result) - Format authentication responses
  • broadcast(array $channels, string $event, array $payload = []) - Send broadcasts to channels

For implementation details of specific broadcasters, see Broadcasting Drivers.

Sources: src/Broadcasters/Broadcaster.php23