VOOZH about

URL: https://deepwiki.com/hypervel/components/8.1-broadcasting-architecture-and-drivers

⇱ Broadcasting Architecture and Drivers | hypervel/components | DeepWiki


Loading...
Last indexed: 7 March 2026 (96fbab)
Menu

Broadcasting Architecture and Drivers

This document describes the core architecture of the broadcasting system, including the BroadcastManager factory, the Broadcaster interface, and the available driver implementations (Pusher, Ably, Redis, Log, Null). It covers driver resolution, connection pooling strategies, and configuration patterns.

For information about channel authentication and authorization mechanisms, see Channel Authentication and Authorization. For event broadcasting workflows and queue integration, see Event Broadcasting and Queuing.


Architecture Overview

The broadcasting system follows a manager-driver pattern where BroadcastManager acts as a factory for creating and caching broadcaster instances. Each broadcaster implements the Broadcaster interface and handles communication with specific real-time services.

Component Hierarchy


Sources: src/broadcasting/src/BroadcastManager.php1-465 src/broadcasting/src/Broadcasters/Broadcaster.php1-305 src/broadcasting/src/Contracts/Broadcaster.php1-26 src/broadcasting/src/BroadcastPoolProxy.php1-45


BroadcastManager: Factory and Driver Resolution

The BroadcastManager class serves as the central factory for creating and managing broadcaster instances. It resolves drivers based on configuration, caches instances, and optionally wraps them in connection pools.

Driver Resolution Flow


Sources: src/broadcasting/src/BroadcastManager.php214-279 src/broadcasting/src/BroadcastManager.php244-259

Key Methods

MethodPurposeLine Reference
driver(?string $name)Get or create a driver instanceBroadcastManager.php224-229
connection(?string $driver)Alias for driver()BroadcastManager.php216-219
resolve(string $name)Resolve driver with pool proxy if applicableBroadcastManager.php244-259
doResolve(array $config)Create actual broadcaster instanceBroadcastManager.php266-279
extend(string $driver, Closure $callback)Register custom driver creatorBroadcastManager.php422-427
purge(?string $name)Remove driver from cacheBroadcastManager.php412-417

Sources: src/broadcasting/src/BroadcastManager.php214-427

Broadcasting Workflow

The manager also handles event queuing and provides convenient methods for initiating broadcasts:


Sources: src/broadcasting/src/BroadcastManager.php138-203


Broadcaster Interface and Base Class

Interface Contract

The Broadcaster interface defines three core methods that all drivers must implement:


Sources: src/broadcasting/src/Contracts/Broadcaster.php9-25

Base Class Responsibilities

The abstract Broadcaster base class provides shared functionality for all drivers:

FeatureDescriptionLine Reference
Channel registrationStore channel auth callbacksBroadcaster.php72-85
Pattern matchingMatch channel names against patternsBroadcaster.php283-286
Parameter extractionExtract route-like parameters from channelsBroadcaster.php119-128
Implicit bindingAuto-resolve Eloquent models from parametersBroadcaster.php189-206
User retrievalGet authenticated user with guard supportBroadcaster.php243-262
Channel optionsStore and retrieve per-channel configurationBroadcaster.php267-278

Sources: src/broadcasting/src/Broadcasters/Broadcaster.php23-304

Channel Pattern Matching

The base class supports Laravel-style route patterns in channel names:


Sources: src/broadcasting/src/Broadcasters/Broadcaster.php165-170 src/broadcasting/src/Broadcasters/Broadcaster.php119-128 tests/Broadcasting/BroadcasterTest.php52-75


Driver Implementations

PusherBroadcaster

Integrates with the Pusher Channels service using the Pusher PHP SDK.

Key Features:

  • Channel name normalization (removes private-, presence-, private-encrypted- prefixes)
  • Socket ID exclusion for preventing echo
  • User authentication for presence channels
  • Batch broadcasting (100 channels per request)
  • Uses UsePusherChannelConventions trait

Configuration Example:


Sources: src/broadcasting/src/Broadcasters/PusherBroadcaster.php1-159 src/broadcasting/public/broadcasting.php51-73 src/broadcasting/src/BroadcastManager.php300-334

AblyBroadcaster

Integrates with the Ably Realtime service using the Ably REST PHP SDK.

Key Features:

  • Channel name formatting (private:, presence:, public: prefixes)
  • HMAC signature generation for authentication
  • Token-based authentication
  • Connection key handling for socket exclusion
  • Uses custom channel normalization

Channel Format Mapping:

Input ChannelFormatted Channel
private-chatprivate:chat
presence-roompresence:room
notificationspublic:notifications

Configuration Example:


Sources: src/broadcasting/src/Broadcasters/AblyBroadcaster.php1-204 src/broadcasting/src/Broadcasters/AblyBroadcaster.php156-170 src/broadcasting/public/broadcasting.php75-84 src/broadcasting/src/BroadcastManager.php339-350

RedisBroadcaster

Broadcasts events via Redis pub/sub channels.

Key Features:

  • Uses Hyperf's RedisFactory for connection management
  • Publishes JSON-encoded payloads
  • Supports Redis key prefixes
  • No authentication required (handles authentication via base class)

Broadcast Flow:


Configuration Example:


Sources: src/broadcasting/src/Broadcasters/RedisBroadcaster.php1-78 src/broadcasting/public/broadcasting.php86-89 src/broadcasting/src/BroadcastManager.php355-363

LogBroadcaster

Logs broadcast events using PSR-3 logger. Useful for development and debugging.

Output Format:

Broadcasting [event.name] on channels [channel1, channel2] with payload:
{
 "key": "value",
 ...
}

Sources: src/broadcasting/src/Broadcasters/LogBroadcaster.php1-39 src/broadcasting/src/BroadcastManager.php368-371

NullBroadcaster

No-op implementation that discards all broadcasts. Used when broadcasting is disabled.

Sources: src/broadcasting/src/Broadcasters/NullBroadcaster.php1-31 src/broadcasting/src/BroadcastManager.php376-379

Driver Comparison Table

DriverUse CaseConnection PooledExternal ServiceAuthentication
PusherProduction real-time appsYesPusher ChannelsToken-based
AblyProduction real-time appsYesAbly RealtimeToken-based
RedisSelf-hosted pub/subNoRedis ServerChannel-based
LogDevelopment/debuggingNoNoneNone
NullTesting/disabledNoNoneNone

Sources: src/broadcasting/src/BroadcastManager.php61-62


Connection Pooling

Pusher and Ably drivers are wrapped in connection pools to reuse connections across requests in the Swoole/coroutine environment.

Pool Architecture


Sources: src/broadcasting/src/BroadcastPoolProxy.php1-45 src/broadcasting/src/BroadcastManager.php41-62 src/broadcasting/src/BroadcastManager.php252-258

Pool Configuration

Pool settings are defined per connection in the configuration:


The HasPoolProxy trait provides pool management functionality:

  • createPoolProxy() - Wraps driver in BroadcastPoolProxy
  • Manages poolable drivers list (default: ['ably', 'pusher'])
  • Supports custom release callbacks

Sources: src/broadcasting/public/broadcasting.php67-72 src/broadcasting/public/broadcasting.php78-83 src/broadcasting/src/BroadcastManager.php41-62

BroadcastPoolProxy Implementation

The BroadcastPoolProxy extends PoolProxy and implements the Broadcaster interface, proxying all method calls through the pool:


Sources: src/broadcasting/src/BroadcastPoolProxy.php12-44


Configuration Structure

Broadcasting configuration is defined in config/autoload/broadcasting.php:

Configuration Schema


Sources: src/broadcasting/public/broadcasting.php1-100

Driver Registration

The ConfigProvider registers the BroadcastManager as the implementation of Factory:


Sources: src/broadcasting/src/ConfigProvider.php1-28


Route Registration

The BroadcastManager provides methods to register authentication routes:

Authentication Routes


Default Middleware:

  • Includes: web middleware group
  • Excludes: VerifyCsrfToken (CSRF protection disabled for these routes)

Method Signatures:

MethodRoutePurposeLine Reference
routes(array $attributes = [])/broadcasting/authChannel authorizationBroadcastManager.php74-95
userRoutes(?array $attributes = null)/broadcasting/user-authUser authenticationBroadcastManager.php100-114
channelRoutes(?array $attributes = null)/broadcasting/authAlias for routes()BroadcastManager.php121-124

Sources: src/broadcasting/src/BroadcastManager.php74-124 src/broadcasting/src/BroadcastController.php1-33 tests/Broadcasting/BroadcastManagerTest.php124-182

Socket ID Extraction

The manager provides a helper to extract the socket ID from requests, used to prevent echo (broadcasting back to the sender):


Sources: src/broadcasting/src/BroadcastManager.php129-134


Custom Driver Registration

The manager supports custom driver registration via the extend() method:


Custom drivers must implement the Broadcaster interface.

Sources: src/broadcasting/src/BroadcastManager.php422-427