highperapp / websockets
High-performance WebSocket server with O(1) indexed broadcasting. Fully compatible with PHP 8.3 & 8.4 for modern async applications.
10
2025-07-05 13:01 UTC
Requires
- php: ^8.3|^8.4
- amphp/amp: ^3.0
- amphp/http-server: ^3.0
- amphp/websocket-server: ^4.0
- psr/log: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.50
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
Suggests
- highperapp/zero-downtime: For zero-downtime deployments with WebSocket connection migration
README
High-performance WebSocket server with O(1) indexed broadcasting and zero-downtime connection preservation. Works standalone or with HighPer Framework.
🔄 Standalone Library: Works independently in any PHP application - no framework required!
✨ Info: Full Amp v3 compatibility with improved type safety and API updates
🚀 Features
âš¡ Indexed Broadcasting
- O(1) Broadcasting: IndexedBroadcaster for constant-time message delivery
- Channel Indexing: Efficient subscriber management per channel
- Zero-Downtime Preservation: Maintain connections during deployments
- Connection Migration: Seamless connection handoff between workers
🎯 Performance Optimizations
- High Performance: Built on AMPHP v3 for true async/await
- Real-time Streaming: Advanced streaming with backpressure handling
- Production-Ready: Enterprise WebSocket server implementation
- Ultra-Low Latency: Sub-millisecond message broadcasting
- Framework Integration: Deep HighPer Framework integration
- Memory Efficient: Optimized connection and channel management
Installation
composer require highperapp/websockets
Quick Start
<?php require 'vendor/autoload.php'; use HighPerApp\HighPer\WebSockets\StreamingWebSocketHandler; use HighPerApp\HighPer\WebSockets\WebSocketServerHandler; use Psr\Log\NullLogger; // Create WebSocket handler with logger $logger = new NullLogger(); $handler = new StreamingWebSocketHandler($logger, [ 'enable_streaming' => true, 'backpressure_limit' => 1000 ]); // Create and start WebSocket server // Recommended: HighPer Container for optimal performance $container = new \HighPerApp\HighPer\Container\Container(); // Alternative: Any PSR-11 compatible container // $container = new \DI\Container(); $server = new WebSocketServerHandler($container, '0.0.0.0', 8080); $server->start();
🎪 Recommended Ecosystem
For optimal performance and seamless integration, use with the HighPer ecosystem:
# Install the complete high-performance stack
composer require highperapp/websockets
composer require highperapp/container
Why HighPer Container?
- âš¡ <0.001ms service resolution - Perfect for high-frequency WebSocket operations
- 🚀 40-60% faster than generic PSR-11 containers
- 🔧 Build-time compilation - Zero runtime overhead
- 🎯 C10M ready - Handles extreme concurrency scenarios
- 🔄 Object pooling - Efficient resource management for persistent connections
Requirements
- PHP 8.3+ or PHP 8.4+ - Full support for both versions
- AMPHP v3+ - Async/await WebSocket server implementation
- ext-json - JSON message encoding/decoding
- PSR Container - For dependency injection (HighPer Container recommended for optimal performance)
- PSR Logger - For logging (any PSR-3 compatible logger)
- Modern PHP Features - Leverages PHP 8.3/8.4 performance improvements
PHP Version Compatibility
This library is fully tested and optimized for:
✅ PHP 8.3 (LTS)
- Full feature support
- Production-ready performance
- All modern PHP 8.3 features utilized
✅ PHP 8.4 (Latest)
- Complete compatibility
- Enhanced performance benefits
- Future-ready implementation
🔧 Modern PHP Features Used
- Strict Types:
declare(strict_types=1)
throughout - Constructor Property Promotion: Clean, concise code
- Union Types: Flexible parameter handling
- Attributes: Modern PHPUnit test annotations
- Performance Optimizations: JIT compiler ready
License
MIT