VOOZH about

URL: https://deepwiki.com/hypervel/support/7-events-and-broadcasting

⇱ Events and Broadcasting | hypervel/support | DeepWiki


Loading...
Menu

Events and Broadcasting

This page provides an overview of the event dispatching and broadcasting systems in the Hypervel support package. The event system enables decoupled communication between application components through an observer pattern, while the broadcasting system extends events to real-time client communication via WebSockets and HTTP protocols.

For detailed documentation on event dispatching, listeners, and testing, see Event System. For broadcasting-specific features including channel management and drivers, see Broadcasting.

System Overview

The package provides two complementary systems for event-driven architecture:

SystemFacadePurposeKey Features
Event SystemEventInternal event dispatchingListener registration, queued events, event resolution, PSR-14 compatible
BroadcastingBroadcastReal-time client communicationWebSocket channels, driver abstraction (Pusher, Ably), channel authentication

These systems work together: events dispatched internally can optionally broadcast to connected clients, enabling real-time UI updates and notifications.

Sources: src/Facades/Event.php1-106 src/Facades/Broadcast.php1-58

Architecture


Sources: src/Facades/Event.php1-106 src/Facades/Broadcast.php1-58

Event System Fundamentals

The Event facade provides a PSR-14 compatible event dispatcher with additional features for queued events and listener management.

Core Operations


Sources: src/Facades/Event.php12-15

Event Lifecycle Methods

MethodPurposeReturn TypeHalts?
dispatch($event, $payload, $halt)Fire event to all listenersobject|stringOptional
until($event, $payload)Fire until first non-null resultmixedYes
push($event, $payload)Register for later dispatchvoidN/A
flush($event)Dispatch all pushed eventsvoidNo

Sources: src/Facades/Event.php12-18

Listener Registration

The event system supports multiple listener registration patterns:

MethodSignatureUse Case
listen()listen($events, $listener, $priority)Register single or multiple events
subscribe()subscribe($subscriber)Register event subscriber class
forget()forget($event)Remove all listeners for event

Sources: src/Facades/Event.php13-24

Broadcasting System Fundamentals

The Broadcast facade manages real-time communication channels and driver configuration. It extends the Manager pattern to support multiple broadcasting backends.

Channel Types


Sources: src/Facades/Broadcast.php14-16

Broadcasting Methods

MethodPurposeChannel TypeReturns
on($channels)Create anonymous event for public channelsPublicAnonymousEvent
private($channel)Create anonymous event for private channelPrivateAnonymousEvent
presence($channel)Create anonymous event for presence channelPresenceAnonymousEvent
event($event)Create pending broadcast for eventAnyPendingBroadcast
queue($event)Queue broadcast for later dispatchAnyvoid

Sources: src/Facades/Broadcast.php14-18

Driver Management

The broadcast manager supports multiple drivers with connection pooling:


Sources: src/Facades/Broadcast.php19-25

Integration: Events to Broadcasting

Events can automatically broadcast to clients by implementing the ShouldBroadcast interface:


Sources: src/Facades/Event.php12 src/Facades/Broadcast.php17-18

Channel Authorization

Private and presence channels require authorization callbacks:


Sources: src/Facades/Broadcast.php10-42

Testing Infrastructure

The Event facade provides comprehensive testing support through the EventFake class:

Test Doubles


Sources: src/Facades/Event.php43-99 src/Testing/Fakes/EventFake.php1-327

EventFake Methods

MethodPurposeParameters
fake($events)Replace dispatcher with fakearray|string - events to fake (empty = all)
fakeExcept($events)Fake all except specified eventsarray|string - events to allow
fakeFor($callable, $events)Temporarily fake during executioncallable, array - events to fake
except($events)Allow events to dispatch normallyarray|string - events to dispatch

Sources: src/Facades/Event.php45-99 src/Testing/Fakes/EventFake.php45-62

Assertion Methods

MethodPurposeUsage
assertDispatched($event, $callback)Assert event was dispatchedPass closure for filtering
assertDispatchedTimes($event, $times)Assert exact dispatch countVerify event count
assertNotDispatched($event, $callback)Assert event was not dispatchedPass closure for filtering
assertNothingDispatched()Assert no events dispatchedVerify silence
assertListening($event, $listener)Assert listener is registeredVerify configuration

Sources: src/Facades/Event.php28-32 src/Testing/Fakes/EventFake.php67-159

Query Methods

The fake provides methods to inspect dispatched events:

MethodReturn TypePurpose
dispatched($event, $callback)CollectionGet all dispatched events matching filter
hasDispatched($event)boolCheck if event was dispatched
dispatchedEvents()arrayGet all dispatched events by type

Sources: src/Facades/Event.php33-35 src/Testing/Fakes/EventFake.php164-318

Code Entity Reference

Primary Classes

ClassFile PathPurpose
Eventsrc/Facades/Event.php40-105Static facade for event dispatcher
Broadcastsrc/Facades/Broadcast.php48-57Static facade for broadcast manager
EventFakesrc/Testing/Fakes/EventFake.php17-327Test double for event system

Container Bindings

FacadeAccessorResolved Type
EventEventDispatcherInterface::classPSR-14 event dispatcher
BroadcastBroadcastingFactoryContract::classBroadcast manager factory

Sources: src/Facades/Event.php101-104 src/Facades/Broadcast.php53-56

Interface Compatibility

The event system implements PSR-14 EventDispatcherInterface, providing standard compliance:

MethodPSR-14Extended Hypervel
dispatch()Adds $halt parameter
listen()-✓ Priority support
until()-✓ Halting dispatch
push()/flush()-✓ Deferred events

Sources: src/Facades/Event.php12-18 src/Testing/Fakes/EventFake.php14

Summary

The events and broadcasting systems provide complementary functionality:

  • Event System: Internal application event dispatching with PSR-14 compatibility
  • Broadcasting: Real-time client communication via WebSockets and HTTP
  • Integration: Events can automatically broadcast to clients
  • Testing: Comprehensive faking infrastructure with assertion methods

For implementation details, see Event System for dispatching and listeners, and Broadcasting for channel management and drivers.

Sources: src/Facades/Event.php1-106 src/Facades/Broadcast.php1-58 src/Testing/Fakes/EventFake.php1-327