xentixar / socklet
Framework-agnostic PHP WebSocket and HTTP server library with attribute-based routing and support for namespaces and rooms.
v1.0.0
2025-05-23 18:35 UTC
Requires
- php: >=8.0
Requires (Dev)
- pestphp/pest: ^3.8
README
Welcome to Socklet! A framework-agnostic PHP WebSocket and HTTP server library that provides attribute-based routing and powerful namespaces and rooms functionality.
Features
- WebSocket and HTTP combined server
- Attribute-based routing for both WebSocket events and HTTP endpoints
- Advanced HTTP request and response handling
- Path parameters and query parameter support
- RESTful API support with content negotiation
- Namespaces and rooms support for WebSocket communication
- Middleware support for authentication and request processing
- Zero dependencies - built with PHP core functionality only
- Easy-to-use event-based architecture
Installation
composer require xentixar/socklet
Quick Start
use Xentixar\Socklet\Core\Server; use Xentixar\Socklet\Core\Contracts\SocketController; use Xentixar\Socklet\WebSocket\Attributes\SocketOn; use Xentixar\Socklet\Http\Attributes\HttpRoute; use Xentixar\Socklet\Http\Request; use Xentixar\Socklet\Http\Response; class MyController extends SocketController { // WebSocket event handler #[SocketOn('message')] public function handleMessage(int $clientId, array $data) { $this->broadcast('message', [ 'from' => $clientId, 'text' => $data['text'] ?? '' ]); } // HTTP route with path parameter #[HttpRoute('GET', '/users/{id}')] public function getUser(Request $request): Response { $userId = $request->getParam('id'); return Response::json([ 'id' => $userId, 'name' => 'Example User' ]); } } // Initialize server $server = new Server("0.0.0.0", 8000); // Register your controller $server->registerController(new MyController()); // Start the server $server->run();
See the example files for complete demonstrations:
examples/example.php
- Basic WebSocket and HTTP exampleexamples/namespace_example.php
- WebSocket namespaces and roomsexamples/advanced_http_example.php
- Advanced HTTP features
Documentation
-
- Installation
- Basic Usage
- Quick Example
-
- Server Setup
- WebSocket Events
- HTTP Routes
- Namespaces & Rooms
- Middleware
-
- Server Class
- WebSocket Handler
- HTTP Handler
- Controllers
- Attributes
-
- Basic Chat Application
- Room Management
- HTTP API Integration
-
- Custom Middleware
- Error Handling
- Best Practices
Requirements
- PHP >= 8.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.