Sockeon Documentation
Welcome to the comprehensive documentation for Sockeon - a framework-agnostic PHP WebSocket and HTTP server library with attribute-based routing and powerful namespaces and rooms functionality.
Table of Contents
Getting Started
Core Components
WebSocket Features
HTTP Features
Data Validation and Sanitization
Advanced Features
API Reference
Examples
Features Overview
- WebSocket and HTTP Combined Server - Single server handling both protocols
- Attribute-based Routing - Clean, declarative routing with PHP 8 attributes
- Namespaces and Rooms - Organized client grouping and broadcasting
- Middleware Support - Flexible request/response processing with HTTP and WebSocket middleware
- Rate Limiting - Built-in protection against abuse with configurable limits
- CORS Support - Configurable cross-origin resource sharing
- PSR-3 Logging - Comprehensive logging with multiple levels
- Zero Dependencies - Built with PHP core functionality only
- PHP Client - Connect to Sockeon servers from PHP applications
- WebSocket Authentication - Key-based authentication for secure connections
- Unified Validation System - Shared validation and sanitization for HTTP and WebSocket data
- Exception Handling - Comprehensive error handling with contextual logging
Requirements
- PHP >= 8.0
- ext-openssl
- ext-sockets
Quick Example
<?php
use Sockeon\Sockeon\Config\ServerConfig;
use Sockeon\Sockeon\Connection\Server;
use Sockeon\Sockeon\Controllers\SocketController;
use Sockeon\Sockeon\Http\Attributes\HttpRoute;
use Sockeon\Sockeon\Http\Request;
use Sockeon\Sockeon\Http\Response;
use Sockeon\Sockeon\WebSocket\Attributes\SocketOn;
use Sockeon\Sockeon\WebSocket\Attributes\OnConnect;
class MyController extends SocketController
{
#[OnConnect]
public function onConnect(int $clientId): void
{
$this->emit($clientId, 'welcome', ['message' => 'Hello!']);
}
#[SocketOn('chat.message')]
public function handleChatMessage(int $clientId, array $data): void
{
$this->broadcast('chat.message', [
'user' => $clientId,
'message' => $data['message']
]);
}
#[HttpRoute('GET', '/api/users')]
public function getUsers(Request $request): Response
{
return Response::json(['users' => ['John', 'Jane']]);
}
}
$config = new ServerConfig();
$config->host = '0.0.0.0';
$config->port = 6001;
$server = new Server($config);
$server->registerController(new MyController());
$server->run();
Community
License
Sockeon is open-sourced software licensed under the MIT license.