VOOZH about

URL: https://deepwiki.com/hypervel/broadcasting/6.3-traits-and-mixins

⇱ Traits & Mixins | hypervel/broadcasting | DeepWiki


Loading...
Menu

Traits & Mixins

This document describes the reusable traits provided by the broadcasting package that encapsulate common functionality patterns. These traits can be composed into event classes and broadcaster implementations to add specific behaviors such as connection selection, socket exclusion, and channel naming conventions.

For information about implementing custom broadcasters that may use these traits, see Custom Broadcasters. For details on authentication and authorization mechanics, see Authentication & Authorization.


Overview

The broadcasting package provides two primary traits that implement the mixin pattern for reusable event functionality:

TraitPurposeKey MethodsUsed By
InteractsWithBroadcastingManages broadcaster connection selection for eventsbroadcastVia(), broadcastConnections()AnonymousEvent, custom event classes
InteractsWithSocketsControls socket ID management and sender exclusiondontBroadcastToCurrentUser(), broadcastToEveryone()AnonymousEvent, custom event classes

These traits enable composition over inheritance, allowing events to selectively adopt broadcasting behaviors without deep inheritance hierarchies. Both traits are used together in AnonymousEvent src/AnonymousEvent.php17-18 to provide a complete fluent broadcasting API.

Sources: src/InteractsWithBroadcasting.php1-36 src/InteractsWithSockets.php1-36 src/AnonymousEvent.php14-19


Trait Composition in AnonymousEvent


AnonymousEvent Trait Composition - This class diagram shows how AnonymousEvent composes the two traits to provide a complete broadcasting interface. The toOthers() method src/AnonymousEvent.php90-95 uses the trait's dontBroadcastToCurrentUser() functionality internally.

Sources: src/AnonymousEvent.php14-18 src/InteractsWithBroadcasting.php9-35 src/InteractsWithSockets.php9-35


InteractsWithBroadcasting Trait

The InteractsWithBroadcasting trait provides connection selection functionality, allowing events to specify which broadcaster connection(s) should handle their broadcast operations.

Implementation Details

The trait maintains a protected property $broadcastConnection src/InteractsWithBroadcasting.php14 that stores an array of connection identifiers. By default, this is initialized to [null], indicating the default connection should be used.

Public API

MethodParametersReturn TypeDescription
broadcastVia()array|string|null $connectionstaticSets the broadcaster connection(s) for the event
broadcastConnections()NonearrayReturns the configured connection identifiers

Method Behavior

broadcastVia($connection) src/InteractsWithBroadcasting.php19-26

This method accepts three input types:

  • null: Resets to default connection [null]
  • string: Single connection name, wrapped in array
  • array: Multiple connection names

The method uses Arr::wrap() src/InteractsWithBroadcasting.php23 to normalize string inputs into arrays, ensuring consistent internal representation. It returns $this for method chaining.

broadcastConnections() src/InteractsWithBroadcasting.php31-34

Returns the raw $broadcastConnection array. The BroadcastManager uses this method to determine which driver instances should process the event.

Usage Pattern

The trait is used by event classes to specify broadcasting connections:


When BroadcastManager processes an event, it calls broadcastConnections() to determine which driver instances should handle the broadcast. If multiple connections are specified, the event is broadcast to all of them sequentially.

Sources: src/InteractsWithBroadcasting.php1-36


InteractsWithSockets Trait

The InteractsWithSockets trait manages socket identification for WebSocket connections, enabling sender exclusion patterns where the originating client does not receive their own broadcast.

Implementation Details

The trait declares a public property $socket src/InteractsWithSockets.php14 that stores the socket ID string. This property is nullable and directly accessible by broadcasting drivers.

Public API

MethodParametersReturn TypeDescription
dontBroadcastToCurrentUser()NonestaticExcludes the current user's socket from broadcast
broadcastToEveryone()NonestaticIncludes all sockets in broadcast

Socket ID Resolution Flow


Socket Resolution Flow - This sequence diagram shows how dontBroadcastToCurrentUser() retrieves the socket ID from the current HTTP request via the Broadcast facade and stores it on the event object.

Sources: src/InteractsWithSockets.php1-36

Method Behavior

dontBroadcastToCurrentUser() src/InteractsWithSockets.php19-24

This method calls Broadcast::socket() to retrieve the socket ID from the current request context. The BroadcastManager.socket() method extracts this from the socket_id request parameter, which WebSocket clients typically send during channel subscription.

When broadcasting occurs, drivers check the $socket property and exclude that socket ID from the recipient list. For Pusher, this is passed as the socket_id parameter src/Broadcasters/PusherBroadcaster.php126-128

broadcastToEveryone() src/InteractsWithSockets.php29-34

This method explicitly sets $socket to null, ensuring all connected clients receive the broadcast regardless of who triggered the event.

Usage Patterns

The trait provides two methods that control the $socket property src/InteractsWithSockets.php14:

Automatic Exclusion in Event Classes:


Dynamic Control:


Integration with AnonymousEvent:

The AnonymousEvent class uses this trait and exposes it via the toOthers() method src/AnonymousEvent.php90-95:


Sources: src/InteractsWithSockets.php1-36 src/AnonymousEvent.php90-95 src/AnonymousEvent.php110-117


Trait Data Flow


Trait Method Execution Flow - This sequence diagram shows how the traits store configuration in the event object and how the BroadcastManager and drivers retrieve that configuration during broadcasting. Both traits provide fluent interfaces that return $this for method chaining.

Sources: src/InteractsWithBroadcasting.php19-26 src/InteractsWithBroadcasting.php31-34 src/InteractsWithSockets.php19-24 src/InteractsWithSockets.php14


Practical Usage Patterns

Event Class Composition

Custom event classes compose both traits to gain full broadcasting control:


AnonymousEvent Implementation

The AnonymousEvent class demonstrates full trait composition src/AnonymousEvent.php16-18:


The via() method src/AnonymousEvent.php56-61 sets a connection property, while the trait's broadcastVia() method is available but not directly exposed. The toOthers() method src/AnonymousEvent.php90-95 manipulates the includeCurrentUser flag, which is then used during send() src/AnonymousEvent.php110-117 to call dontBroadcastToCurrentUser().

Complete Fluent Example


Sources: src/AnonymousEvent.php16-18 src/AnonymousEvent.php56-61 src/AnonymousEvent.php90-95 src/AnonymousEvent.php110-117 src/InteractsWithBroadcasting.php1-36 src/InteractsWithSockets.php1-36


Trait Properties and State Management

Both traits manage internal state through protected/public properties:

InteractsWithBroadcasting State


The $broadcastConnection property src/InteractsWithBroadcasting.php14 stores connection names as an array. When broadcastVia() receives:

InteractsWithSockets State


The $socket property src/InteractsWithSockets.php14 is public, allowing broadcasters to read it directly. The property stores the socket ID string obtained from the current HTTP request's socket_id parameter.

Sources: src/InteractsWithBroadcasting.php14 src/InteractsWithBroadcasting.php19-26 src/InteractsWithSockets.php14 src/InteractsWithSockets.php19-24


Best Practices

Trait Selection Guidelines

Use InteractsWithBroadcasting when:

  • Events need to broadcast to specific connections
  • Multi-driver broadcasting is required
  • Connection selection must be dynamic

Use InteractsWithSockets when:

  • Events originate from user actions
  • Sender should not receive their own broadcast
  • WebSocket client identification is available

Use UsePusherChannelConventions when:

  • Implementing a broadcaster with Pusher-compatible naming
  • Supporting private- and presence- channel prefixes
  • Maintaining interoperability with Pusher clients

Initialization Timing

Connection and socket configuration should occur during event construction or immediately after instantiation:


Construction-time configuration ensures consistent behavior regardless of how the event is dispatched.

Method Chaining

All trait methods return $this, enabling fluent method chaining:


This pattern improves code readability and reduces boilerplate.

Sources: src/InteractsWithBroadcasting.php1-36 src/InteractsWithSockets.php1-36 src/Broadcasters/UsePusherChannelConventions.php1-33