VOOZH about

URL: https://deepwiki.com/hypervel/broadcasting/3.2-pusher-broadcaster

⇱ Pusher Broadcaster | hypervel/broadcasting | DeepWiki


Loading...
Menu

Pusher Broadcaster

Purpose and Scope

The PusherBroadcaster class provides integration with the Pusher Channels API and Reverb (Laravel's Pusher-compatible server). This broadcaster handles authentication for private and presence channels, user authentication for socket connections, and publishes events to the Pusher service with support for batch operations and socket exclusion.

This page covers the Pusher-specific implementation details. For general broadcaster concepts and the base authentication system, see Broadcaster Base Class. For other driver implementations, see Ably Broadcaster, Redis Broadcaster, and Utility Broadcasters.

Sources: src/Broadcasters/PusherBroadcaster.php1-159

Architecture & Dependencies

The PusherBroadcaster extends the abstract Broadcaster base class and uses the UsePusherChannelConventions trait for channel name manipulation. It depends on the official pusher/pusher-php-server SDK for communication with the Pusher service.

Class Structure


Sources: src/Broadcasters/PusherBroadcaster.php16-27

Constructor Dependencies

The broadcaster receives two dependencies via constructor injection:

DependencyTypePurpose
$containerContainerInterfaceService container for dependency resolution
$pusherPusherPusher PHP SDK instance for API communication

The Pusher instance is typically configured with authentication credentials (app ID, key, secret) and connection settings (cluster, encryption) via the configuration system.

Sources: src/Broadcasters/PusherBroadcaster.php23-27

Channel Naming Conventions

The UsePusherChannelConventions trait provides methods for working with Pusher's channel naming patterns. Pusher uses channel prefixes to denote security requirements:

Channel TypePrefixExampleAuthentication Required
Publicnonechat-roomNo
Privateprivate-private-user.1Yes
Private Encryptedprivate-encrypted-private-encrypted-orders.5Yes
Presencepresence-presence-team.7Yes

Trait Methods


Sources: src/Broadcasters/UsePusherChannelConventions.php1-33

isGuardedChannel() Method

Determines if a channel requires authentication by checking if the channel name starts with private- or presence-:

src/Broadcasters/UsePusherChannelConventions.php14-17

Sources: src/Broadcasters/UsePusherChannelConventions.php14-17

normalizeChannelName() Method

Strips the security prefix from a channel name to obtain the base channel identifier used for authorization callback matching:

src/Broadcasters/UsePusherChannelConventions.php22-31

The method checks prefixes in order of specificity: private-encrypted-, private-, presence-.

Sources: src/Broadcasters/UsePusherChannelConventions.php22-31

User Authentication

Pusher supports user authentication for socket connections, enabling features like presence channels and user-specific events. The resolveAuthenticatedUser() method generates authentication signatures for WebSocket connections.

Authentication Flow


Sources: src/Broadcasters/PusherBroadcaster.php35-62

Signature Generation

The method first calls the parent resolveAuthenticatedUser() to retrieve the authenticated user data. If no user is authenticated, it returns null src/Broadcasters/PusherBroadcaster.php37-39

For authenticated users, the method checks if the Pusher SDK provides an authenticateUser() method (newer SDK versions). If available, it delegates to the SDK src/Broadcasters/PusherBroadcaster.php41-46

For older SDK versions, it manually generates the authentication signature:

  1. Retrieves Pusher settings (auth key and secret)
  2. Encodes user data as JSON
  3. Creates signature string: {socket_id}::user::{encoded_user}
  4. Generates HMAC-SHA256 hash using the secret
  5. Returns formatted response with auth signature and user data

src/Broadcasters/PusherBroadcaster.php48-61

Sources: src/Broadcasters/PusherBroadcaster.php35-62

Channel Authentication

The PusherBroadcaster implements channel-specific authentication for private and presence channels. The authentication flow involves two methods: auth() for initial validation and validAuthenticationResponse() for generating signed responses.

Authentication Methods


Sources: src/Broadcasters/PusherBroadcaster.php69-111

auth() Method

Validates the incoming authentication request:

  1. Extracts channel_name from request input
  2. Normalizes the channel name by removing prefixes
  3. Checks if the channel name is empty
  4. For guarded channels (private/presence), verifies user authentication
  5. Delegates to parent verifyUserCanAccessChannel() for authorization

src/Broadcasters/PusherBroadcaster.php69-84

If validation fails, an AccessDeniedHttpException is thrown.

Sources: src/Broadcasters/PusherBroadcaster.php69-84

validAuthenticationResponse() Method

Generates the Pusher-specific authentication response after successful authorization. The response format differs based on channel type:

Private Channels

For channels starting with private, the method calls pusher->authorizeChannel() with the channel name and socket ID src/Broadcasters/PusherBroadcaster.php94-98

Presence Channels

For presence channels, the method:

  1. Retrieves the authenticated user
  2. Extracts the broadcast identifier (supports custom getAuthIdentifierForBroadcasting() method)
  3. Calls pusher->authorizePresenceChannel() with channel name, socket ID, user identifier, and authorization result

src/Broadcasters/PusherBroadcaster.php100-110

All responses are decoded from JSON before returning src/Broadcasters/PusherBroadcaster.php116-119

Sources: src/Broadcasters/PusherBroadcaster.php89-119

Broadcasting Messages

The broadcast() method publishes events to Pusher channels with support for batch operations and socket exclusion.

Broadcasting Flow


Sources: src/Broadcasters/PusherBroadcaster.php124-141

Implementation Details

StepOperationPurpose
1. Socket ExtractionArr::pull($payload, 'socket')Removes and captures socket ID for exclusion
2. Parameter Building['socket_id' => $socket]Creates parameters for Pusher API (excludes sender)
3. Channel Formatting$this->formatChannels($channels)Applies channel naming conventions
4. Collection CreationCollection::make(...)Wraps channels for batch operations
5. Chunking->chunk(100)Splits channels into batches of 100 (Pusher API limit)
6. Broadcasting$pusher->trigger(...)Sends event to each batch via Pusher API

src/Broadcasters/PusherBroadcaster.php124-141

Batch Processing

Pusher's API has a limit on the number of channels that can be triggered in a single request. The broadcaster chunks channels into groups of 100 and processes each batch sequentially src/Broadcasters/PusherBroadcaster.php133-135

Socket Exclusion

When the socket parameter is present in the payload, it is extracted and passed to Pusher as socket_id. This excludes the originating connection from receiving the broadcast, preventing echo effects in real-time applications src/Broadcasters/PusherBroadcaster.php126-128

Error Handling

API errors from the Pusher SDK (ApiErrorException) are caught and wrapped in a BroadcastException with a descriptive error message src/Broadcasters/PusherBroadcaster.php136-140

Sources: src/Broadcasters/PusherBroadcaster.php124-141

Pusher SDK Integration

The broadcaster provides getter and setter methods for accessing and replacing the underlying Pusher SDK instance.

SDK Accessor Methods

MethodReturn TypePurpose
getPusher()PusherReturns the current Pusher SDK instance
setPusher(Pusher $pusher)voidReplaces the Pusher SDK instance

src/Broadcasters/PusherBroadcaster.php146-157

These methods are primarily used for:

  • Testing (injecting mock Pusher instances)
  • Object pooling (swapping connections in pool proxies)
  • Runtime configuration changes

Sources: src/Broadcasters/PusherBroadcaster.php146-157

Configuration Example

The PusherBroadcaster is typically configured in config/autoload/broadcasting.php:


For connection pooling configuration details, see Object Pooling & Performance.

Sources: Based on typical Hyperf configuration patterns