Client API Reference
Sockeon's PHP client entrypoint is Sockeon\Sockeon\Connection\Client.
Class: Sockeon\Sockeon\Connection\Client
Constructor
php
public function __construct(string $host, int $port, string $path = '/', int $timeout = 10)Creates a client for a WebSocket endpoint.
Connection methods
php
public function connect(array $headers = []): bool
public function disconnect(): bool
public function isConnected(): boolEvent methods
php
public function on(string $event, callable $callback): self
public function emit(string $event, array $data = []): boolRuntime methods
php
public function run(?callable $callback = null, int $checkInterval = 50): void
public function setAutoReconnect(bool $enabled, int $maxAttempts = 5, int $delay = 3): self
public function resetReconnectAttempts(): selfExample
php
use Sockeon\Sockeon\Connection\Client;
use RuntimeException;
$client = (new Client('localhost', 6001))
->setAutoReconnect(true, 5, 3)
->on('welcome', function (array $data): void {
echo "Server says: " . ($data['message'] ?? 'hello') . PHP_EOL;
})
->on('chat.message', function (array $data): void {
echo '[' . ($data['from'] ?? 'unknown') . '] ' . ($data['message'] ?? '') . PHP_EOL;
});
if (!$client->connect()) {
throw new RuntimeException('Connection failed');
}
$client->emit('chat.join', ['room' => 'general']);
$client->emit('chat.message', ['message' => 'Hello from PHP client']);
$client->run();Important Notes
Clientdoes not expose low-level receive APIs likereceive()orreceiveEvent().Clientalso does not include callback helpers likeonMessage()or timeout mutators likesetTimeout().- Use
on()callbacks plusrun()for long-lived listening behavior. emit()expects Sockeon event format (event+data) and returnsfalseon failure.