hyperf/event
hyperf/event is a lightweight event dispatcher for Hyperf applications. Define events and listeners, dispatch synchronously or via async mechanisms, and keep your domain decoupled. Integrates cleanly with Hyperf’s DI and coroutine-friendly runtime.
hyperf/event package bridges Laravel’s event system with Hyperf’s high-performance coroutine-based architecture, enabling sub-millisecond latency for event-driven workflows. This is ideal for real-time systems (e.g., live dashboards, gaming backends) where Laravel’s traditional event system would introduce I/O bottlenecks.LaravelEventAdapter) introduces a dual-stack approach. This allows Laravel apps to leverage Hyperf’s Swoole coroutines without rewriting event logic, but requires careful dependency isolation to avoid conflicts (e.g., Laravel’s EventServiceProvider vs. Hyperf’s EventDispatcher).EventWebSocket support for binary payloads (e.g., protobuf) is a game-changer for high-throughput applications like collaborative tools or multiplayer games, where JSON serialization overhead is prohibitive.ShouldQueue but adds Hyperf’s coroutine-based execution, reducing latency for high-priority events (e.g., fraud alerts). However, this feature is Hyperf-specific and may not translate cleanly to Laravel’s queue system.LaravelEventAdapter is GA but untested in production for edge cases (e.g., custom event dispatchers, nested transactions). The fallback-to-Redis mechanism reduces risk but adds latency overhead if Redis is slow.pcntl, opcache). Benchmarking is critical to ensure Hyperf’s coroutines don’t block Laravel’s synchronous I/O.EventWebSocket are compelling, but real-world performance depends on:
EventTransaction middleware’s compatibility with Laravel’s transactionLevel() is untested for complex workflows (e.g., distributed transactions). Idempotency risks may arise if events are retried during failures.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Adapter Stability | High | Start with non-critical events (e.g., analytics) before migrating core workflows. Use FALLBACK_TO_REDIS=true as a safety net. |
| Swoole Conflicts | Medium | Isolate Hyperf workers in separate PHP processes (e.g., via RoadRunner). Monitor php:memory_usage for leaks. |
| WebSocket Scaling | High | Load-test with 10K+ concurrent connections before production. Use hyperf:event:bench to validate claims. |
| Debugging Complexity | Medium | Integrate hyperf:event:trace with Laravel Telescope for unified observability. |
| Binary Payload Limits | Low | Test with max payload size (e.g., 1MB protobuf) to ensure WebSocket compatibility. |
| Edge Cold Starts | Medium | Deploy Hyperf workers in warm pools (e.g., AWS Fargate) to mitigate cold starts. |
EventServiceProvider, or does it coexist? If the latter, how are conflicts resolved (e.g., duplicate listeners)?UserObserver), or must all logic be moved to listeners?Bus or ShouldBroadcastNow (no direct support).Phase 0: Infrastructure Prep
RoadRunner or Docker) to avoid Swoole conflicts.Phase 1: Pilot with Non-Critical Events
UserLoggedIn) to Hyperf.hyperf:event:bench.Phase 2: WebSocket Integration (Optional)
EventWebSocket for a non-critical channel (e.g., notifications).Phase 3: Priority Queues
AdjustPriorityMiddleware for dynamic priorities (e.g., fraud detection).Phase 4: Full Event Mesh
EventWebSocket for real-time features.$eventManager->route('order.processed', fn($event) => "tenant_{$event->tenantId}");
Phase 5: Observability
hyperf:event:trace with Jaeger/OpenTelemetry and Laravel Telescope.EventConflictResolver invocations (indicates duplicates).Bus or ShouldBroadcastNow.How can I help you explore Laravel packages today?