VOOZH about

URL: https://deepwiki.com/hypervel/broadcasting/6-advanced-topics

⇱ Advanced Topics | hypervel/broadcasting | DeepWiki


Loading...
Menu

Advanced Topics

This section covers advanced features and extension points of the hypervel/broadcasting system. These topics are intended for developers who need to customize the broadcasting infrastructure, optimize performance for high-throughput scenarios, implement custom authentication logic, or create new broadcaster implementations.

The material in this section builds upon the foundational concepts covered in Core Architecture and assumes familiarity with the standard broadcasting patterns described in Event Broadcasting. For basic usage and configuration, see Basic Usage.

The following advanced topics are covered:

  • Authentication & Authorization (6.1): Deep dive into channel authentication mechanics, HTTP routes, guard configuration, and implicit model binding
  • Object Pooling & Performance (6.2): Connection pooling strategies for remote broadcasters, pool proxy implementation, and configuration
  • Traits & Mixins (6.3): Helper traits for connection management, socket filtering, and channel naming conventions
  • Custom Broadcasters (6.4): Guidelines for implementing custom broadcaster drivers and registering them with the factory

System Extension Architecture

The broadcasting system provides multiple extension points that allow customization without modifying core classes. Understanding these extension mechanisms is essential for advanced usage.


Extension Points Overview

Sources: src/BroadcastManager.php426-431 src/Broadcasters/Broadcaster.php72-85

The system provides four primary extension mechanisms:

Extension PointClass/LocationPurposeRegistration Method
Custom DriversBroadcastManager::$customCreatorsRegister new broadcaster implementationsBroadcastManager::extend()
Pool ProxiesHasPoolProxy traitWrap drivers with connection poolingAdd to $poolables array
Channel AuthenticatorsBroadcaster::$channelsDefine channel authorization logicBroadcaster::channel()
User ResolutionBroadcaster::$authenticatedUserCallbackCustomize user authenticationBroadcaster::resolveAuthenticatedUserUsing()

Sources: src/BroadcastManager.php41-56 src/Broadcasters/Broadcaster.php25-44


Driver Factory Pattern

The BroadcastManager implements a factory pattern with support for custom driver registration. The resolution flow checks for custom drivers before falling back to built-in implementations.


Driver Resolution Logic

The resolution sequence follows this order:

  1. Check driver cache at BroadcastManager::$drivers[$name] (src/BroadcastManager.php232)
  2. Look up configuration at broadcasting.connections.{$name} (src/BroadcastManager.php391)
  3. Check if driver is in $customCreators array (src/BroadcastManager.php272)
  4. If custom, invoke custom creator closure (src/BroadcastManager.php288-291)
  5. Otherwise, call create{Driver}Driver() method (src/BroadcastManager.php276-282)
  6. If driver is poolable, wrap with BroadcastPoolProxy (src/BroadcastManager.php256-262)

Sources: src/BroadcastManager.php228-283

Custom Driver Registration

Custom drivers are registered using the extend() method:


The closure receives two parameters:

  • ContainerInterface $app - DI container instance
  • array $config - Driver configuration from broadcasting.connections.{name}

Sources: src/BroadcastManager.php425-431 src/BroadcastManager.php288-291


Authentication System Architecture

Channel authentication is handled through a static registry of patterns and callbacks registered on the Broadcaster base class. This architecture allows all broadcaster instances to share authentication logic while delegating response formatting to driver-specific implementations.


Authentication Flow Sequence

Sources: src/Broadcasters/Broadcaster.php72-114

The authentication process follows these steps:

  1. Pattern Matching: Iterate through Broadcaster::$channels to find matching pattern (src/Broadcasters/Broadcaster.php94-97)
  2. Parameter Extraction: Parse channel wildcards (e.g., {id}) from pattern (src/Broadcasters/Broadcaster.php99 src/Broadcasters/Broadcaster.php165-169)
  3. Implicit Model Binding: Resolve model instances for UrlRoutable parameters (src/Broadcasters/Broadcaster.php189-206)
  4. User Retrieval: Get authenticated user via configured guard(s) (src/Broadcasters/Broadcaster.php243-262)
  5. Callback Invocation: Execute authorization callback with user and parameters (src/Broadcasters/Broadcaster.php103)
  6. Response Formatting: Driver-specific formatting via validAuthenticationResponse() (implemented in subclasses)

Sources: src/Broadcasters/Broadcaster.php92-114


Performance Optimization Strategy

For broadcasters that communicate with remote services (Pusher, Ably), the system implements connection pooling through the HasPoolProxy trait and BroadcastPoolProxy wrapper class. This reduces connection overhead in high-throughput scenarios.

Poolable Drivers

The BroadcastManager defines which drivers support pooling:


Sources: src/BroadcastManager.php61

When a poolable driver is resolved, the manager wraps it with a pool proxy:


Sources: src/BroadcastManager.php256-262

Pool Proxy Pattern


Pool Configuration

Pool parameters are specified in the connection configuration:

ParameterTypeDefaultDescription
pool.min_connectionsint1Minimum pool size
pool.max_connectionsint10Maximum pool size
pool.connect_timeoutfloat10.0Connection timeout (seconds)
pool.wait_timeoutfloat3.0Pool wait timeout (seconds)
pool.heartbeatint-1Connection validation interval
pool.max_idle_timefloat60.0Maximum idle time before cleanup

Sources: src/BroadcastManager.php28

For detailed pool configuration and performance tuning, see Object Pooling & Performance.


Helper Traits System

The package provides three helper traits that encapsulate common broadcasting patterns:


Trait Purposes

TraitPurposePrimary Methods
InteractsWithBroadcastingSpecify broadcast connections for eventsbroadcastVia(), broadcastConnections()
InteractsWithSocketsControl which socket connections receive broadcastsdontBroadcastToCurrentUser(), broadcastToEveryone()
UsePusherChannelConventionsShared channel name utilities for Pusher-compatible driversisGuardedChannel(), normalizeChannelName(), formatChannels()

For detailed documentation of each trait and usage examples, see Traits & Mixins.


Custom Broadcaster Development

Implementing a custom broadcaster requires extending the Broadcaster base class and implementing the Contracts\Broadcaster interface methods.

Required Methods


Implementation Checklist

  1. Extend Hypervel\Broadcasting\Broadcasters\Broadcaster base class
  2. Implement Hypervel\Broadcasting\Contracts\Broadcaster interface
  3. Implement auth(RequestInterface $request) - channel authentication endpoint handler
  4. Implement validAuthenticationResponse($request, $result) - format authentication response
  5. Implement broadcast(array $channels, $event, $payload) - publish event to channels
  6. Register with BroadcastManager::extend($driver, Closure $callback)

Registration Example Pattern

Custom broadcasters are registered via the extend() method, typically in a service provider or configuration:


Sources: src/BroadcastManager.php425-431

The closure receives the DI container and driver configuration, allowing access to dependencies and settings defined in broadcasting.connections.custom.

For complete implementation guidance and examples, see Custom Broadcasters.


Summary

The advanced features of hypervel/broadcasting provide powerful extension mechanisms while maintaining backward compatibility and ease of use. The key architectural principles are:

  • Factory Pattern: Custom drivers integrate seamlessly through the extend() method
  • Static Registry: Channel authenticators are shared across all broadcaster instances
  • Pool Proxies: Performance optimization is transparent to application code
  • Helper Traits: Common patterns are extracted into reusable mixins
  • Base Class Support: Authentication logic is centralized in the Broadcaster base class

For implementation details, refer to the subsections of this chapter.

Sources: src/BroadcastManager.php1-468 src/Broadcasters/Broadcaster.php1-304