VOOZH about

URL: https://deepwiki.com/hypervel/broadcasting/3.4-utility-broadcasters

⇱ Utility Broadcasters | hypervel/broadcasting | DeepWiki


Loading...
Menu

Utility Broadcasters

Purpose and Scope

This document covers the utility broadcaster implementations: LogBroadcaster and NullBroadcaster. These are non-production broadcasting drivers designed for debugging, testing, and selectively disabling broadcast functionality. Unlike production drivers that publish messages to external services or Redis (see Ably Broadcaster, Pusher Broadcaster, and Redis Broadcaster), utility broadcasters provide simplified implementations for development and testing scenarios.

For information about the base Broadcaster class and its template methods, see Broadcaster Base Class.


Overview

The broadcasting system includes two utility drivers that serve distinct purposes during development and testing:

BroadcasterPurposeOutput Behavior
LogBroadcasterDebugging and development monitoringWrites broadcast events to the application log
NullBroadcasterTesting and feature disablingSilently discards all broadcast events

Both broadcasters extend the abstract Broadcaster class but provide minimal implementations that bypass authentication and external service communication.

Sources: src/Broadcasters/LogBroadcaster.php10 src/Broadcasters/NullBroadcaster.php9


Class Hierarchy


Both utility broadcasters extend the Broadcaster base class and implement the minimum required methods. The LogBroadcaster requires a LoggerInterface dependency for output, while NullBroadcaster has no dependencies.

Sources: src/Broadcasters/LogBroadcaster.php10-18 src/Broadcasters/NullBroadcaster.php9


LogBroadcaster

Purpose

The LogBroadcaster provides a development-friendly way to observe broadcast activity without requiring external service setup. It writes human-readable broadcast information to the application's log system, making it ideal for:

  • Local development and debugging
  • Verifying broadcast event structure
  • Monitoring channel and payload data
  • Troubleshooting event dispatching issues

Sources: src/Broadcasters/LogBroadcaster.php10

Implementation

The LogBroadcaster implementation is straightforward:


Sources: src/Broadcasters/LogBroadcaster.php30-37

Constructor Dependency

The broadcaster requires a LoggerInterface instance:

__construct(protected LoggerInterface $logger)

The logger is injected through Hyperf's dependency injection container and defaults to the application's configured log handler.

Sources: src/Broadcasters/LogBroadcaster.php15-18

Broadcast Method

The broadcast() method implements the following logic:

  1. Channel Formatting: Calls inherited formatChannels() method to normalize channel names
  2. Channel Concatenation: Joins channel names with comma-space separator
  3. Payload Encoding: JSON encodes the payload with JSON_PRETTY_PRINT for readability
  4. Log Output: Writes formatted message to logger at INFO level

The output format is:

Broadcasting [{event}] on channels [{channels}] with payload:
{json_payload}

Sources: src/Broadcasters/LogBroadcaster.php30-37

Log Output Example

For a broadcast event with channels ['orders.1', 'notifications'] and event name OrderShipped:

Broadcasting [OrderShipped] on channels [orders.1, notifications] with payload:
{
 "order": {
 "id": 1,
 "status": "shipped"
 },
 "socket": null
}

Sources: src/Broadcasters/LogBroadcaster.php32-36

Configuration

To use the log broadcaster, set the driver in config/autoload/broadcasting.php:

Configuration KeyValueDescription
default'log'Sets log as the default broadcast driver
connections.log.driver'log'Identifies the log driver connection

The log broadcaster requires no additional configuration keys beyond the driver specification.


NullBroadcaster

Purpose

The NullBroadcaster provides a no-operation implementation that silently discards all broadcast requests. This is useful for:

  • Testing scenarios where broadcasts should be suppressed
  • Temporarily disabling broadcasting without code changes
  • Continuous integration environments
  • Feature flag implementations

Sources: src/Broadcasters/NullBroadcaster.php9

Implementation

The NullBroadcaster is the simplest broadcaster implementation:


All methods return immediately without performing any operations.

Sources: src/Broadcasters/NullBroadcaster.php21-23

Method Implementations

MethodImplementationReturn Value
auth()Empty - returns nullnull
validAuthenticationResponse()Empty - returns nullnull
broadcast()Empty - no operationvoid

The broadcast() method has an empty body, meaning events are accepted but immediately discarded.

Sources: src/Broadcasters/NullBroadcaster.php11-23

Configuration

To use the null broadcaster, configure it in config/autoload/broadcasting.php:

Configuration KeyValueDescription
default'null'Sets null as the default broadcast driver
connections.null.driver'null'Identifies the null driver connection

No additional configuration is required. The null broadcaster has no dependencies and no external connections.


Authentication Behavior

Both utility broadcasters implement stub authentication methods:


No Authentication Support

Both broadcasters return null from their authentication methods:

  • auth(RequestInterface $request): mixed → returns null
  • validAuthenticationResponse(RequestInterface $request, mixed $result): mixed → returns null

This behavior indicates that utility broadcasters do not implement channel authentication. They are intended for development/testing environments where authentication is not required.

Sources: src/Broadcasters/LogBroadcaster.php20-28 src/Broadcasters/NullBroadcaster.php11-19

Production Warning

Because utility broadcasters bypass authentication, they should never be used in production environments where private or presence channels require access control. For production broadcasting with authentication, use the Ably, Pusher, or Redis broadcasters (see Channel Types & Authentication).


Configuration Examples

Broadcasting Configuration File

Both broadcasters are pre-configured in the default broadcasting.php configuration:

'connections' => [
 // ... other connections ...
 
 'log' => [
 'driver' => 'log',
 ],
 
 'null' => [
 'driver' => 'null',
 ],
],

Switching Drivers

Using Environment Variables

Set the default driver via the BROADCAST_CONNECTION environment variable:


Programmatic Selection

Select a driver at runtime using the BroadcastManager:

// Use log broadcaster
$broadcaster = $manager->driver('log');

// Use null broadcaster
$broadcaster = $manager->driver('null');

Sources: src/Broadcasters/LogBroadcaster.php10 src/Broadcasters/NullBroadcaster.php9


Use Case Comparison


LogBroadcaster Scenarios

  • Local Development: Monitor broadcast events in log files without external service setup
  • Debugging: Inspect event names, channels, and payload structure
  • Integration Testing: Verify broadcast calls are made with correct data
  • Demo Environments: Show broadcast activity without requiring service credentials

NullBroadcaster Scenarios

  • Unit Testing: Prevent actual broadcasting during test execution
  • CI/CD Pipelines: Avoid external service dependencies in automated builds
  • Feature Flags: Temporarily disable broadcasting for specific features
  • Maintenance Mode: Suppress broadcasts during system maintenance
  • Performance Testing: Isolate application performance without broadcast overhead

Sources: src/Broadcasters/LogBroadcaster.php30-37 src/Broadcasters/NullBroadcaster.php21-23


Comparison Table

FeatureLogBroadcasterNullBroadcaster
DependenciesLoggerInterfaceNone
OutputWrites to application logNo output
PerformanceMinimal overhead (logging I/O)Zero overhead
AuthenticationNot implemented (returns null)Not implemented (returns null)
Use CaseDebugging and monitoringTesting and disabling
Channel FormattingYes (formats and joins channels)No (discards input)
Payload ProcessingJSON encodes with pretty printNo processing
Log LevelINFON/A
Production ReadyNoNo

Sources: src/Broadcasters/LogBroadcaster.php1-38 src/Broadcasters/NullBroadcaster.php1-24


Driver Resolution

Both utility broadcasters are resolved through the BroadcastManager factory pattern:


The driver resolution follows the standard factory pattern (see BroadcastManager) with automatic dependency injection for the LogBroadcaster's LoggerInterface dependency.

Sources: src/Broadcasters/LogBroadcaster.php15-18 src/Broadcasters/NullBroadcaster.php9


Implementation Notes

LogBroadcaster Internals

The LogBroadcaster leverages several inherited and internal mechanisms:

  1. Channel Formatting: Inherits formatChannels() from Broadcaster base class to normalize channel names
  2. String Concatenation: Uses implode(', ', ...) for readable channel list
  3. JSON Encoding: Applies JSON_PRETTY_PRINT flag for multi-line, indented output
  4. Logger Integration: Delegates to PSR-3 LoggerInterface for actual log writing

Sources: src/Broadcasters/LogBroadcaster.php30-37

NullBroadcaster Internals

The NullBroadcaster is intentionally minimal:

  • No constructor parameters
  • No instance variables
  • No method logic
  • All methods return immediately

This design minimizes memory footprint and execution time when broadcasting is disabled.

Sources: src/Broadcasters/NullBroadcaster.php9-24

Performance Characteristics

AspectLogBroadcasterNullBroadcaster
Memory UsageLogger instance + formatted stringsZero additional memory
CPU UsageJSON encoding + string operationsZero CPU cycles
I/O OperationsLog file writesNo I/O
Latency ImpactDepends on log handler (file, syslog, etc.)Zero latency

For performance-critical scenarios where broadcasting must be disabled, NullBroadcaster is preferred over LogBroadcaster.

Sources: src/Broadcasters/LogBroadcaster.php30-37 src/Broadcasters/NullBroadcaster.php21-23