Skip to content

API Reference

Muhammet Şafak edited this page May 24, 2026 · 1 revision

API Reference

A single-page index of every public type. For the deeper "why", see the linked concept pages.

Factory

InitPHP\Socket\Socket

finalclass Socket
{
publicstaticfunctionserver(
Transport$transport,
string$host,
int$port,
?Domain$domain = null,
?float$timeout = null,
): SocketServerInterface;
publicstaticfunctionclient(
Transport$transport,
string$host,
int$port,
?Domain$domain = null,
?float$timeout = null,
): SocketClientInterface;
}

Domain is honoured only by TCP / UDP. $timeout is honoured only by TLS / SSL.

Server contracts

InitPHP\Socket\Interfaces\SocketServerInterface

interface SocketServerInterface
{
publicfunctiongetHost(): string;
publicfunctiongetPort(): int;
publicfunctiongetSocket(): mixed; // \Socket | resource | null/** @return array<int|string, SocketConnectionInterface> */publicfunctiongetClients(): array;
publicfunctionlisten(): static;
publicfunctionclose(): bool;
publicfunctionlive(callable$callback, float$idleSeconds = 0.05): void;
publicfunctiontick(callable$callback, float$waitSeconds = 0.0): int;
publicfunctionstop(): void;
publicfunctionisRunning(): bool;
/** @param int|string|array<int, int|string>|null $clients */publicfunctionbroadcast(string$message, int|string|array|null$clients = null): bool;
publicfunctionregister(int|string$id, SocketConnectionInterface$client): bool;
publicfunctionwait(float$seconds): void;
}

InitPHP\Socket\Server\AbstractServer

Adds:

abstractclass AbstractServer implements SocketServerInterface
{
protectedfunctionaddClient(SocketConnectionInterface$client): int; // returns the internal keyprotectedfunctionevict(int$key): void;
protectedfunctionindexOf(SocketConnectionInterface$client): ?int;
protectedstaticfunctionsplitSeconds(float$seconds): array; // [whole, microseconds]
}

InitPHP\Socket\Server\TCP / UDP

Concrete ext-sockets-backed servers. TCP additionally exposes:

publicfunction backlog(int$backlog): self; // default 8, must be >= 1

InitPHP\Socket\Server\AbstractStreamServer

The parent of TLS and SSL. Adds:

publicfunction option(string$key, mixed$value): static;
publicfunction timeout(float$seconds): static;
publicfunction blocking(bool$mode = true): static;
publicfunction crypto(?CryptoMethod$method): static;

TLS and SSL themselves carry no extra public surface — see Transport TLS / Transport SSL.

Client contracts

InitPHP\Socket\Interfaces\SocketClientInterface

interface SocketClientInterface
{
publicfunctiongetHost(): string;
publicfunctiongetPort(): int;
publicfunctiongetSocket(): mixed;
publicfunctionconnect(): static;
publicfunctiondisconnect(): bool;
publicfunctionread(int$length = 1024): ?string;
publicfunctionwrite(string$data): ?int;
}

InitPHP\Socket\Client\TCP

publicfunction read(int$length = 1024, int$type = PHP_BINARY_READ): ?string;

($type accepts PHP_BINARY_READ or PHP_NORMAL_READ.)

InitPHP\Socket\Client\UDP

publicfunction read(int$length = 1024, int$flags = 0): ?string;
publicfunction write(string$data, int$flags = 0): ?int;

($flags accepts the standard MSG_* bitmasks.)

InitPHP\Socket\Client\AbstractStreamClient

The parent of Client\TLS and Client\SSL. Adds:

publicfunction option(string$key, mixed$value): static;
publicfunction timeout(float$seconds): static;
publicfunction blocking(bool$mode = true): static;
publicfunction crypto(?CryptoMethod$method): static;

Per-connection contract

InitPHP\Socket\Interfaces\SocketConnectionInterface

interface SocketConnectionInterface
{
publicfunctionsetId(int|string$id): static;
publicfunctiongetId(): int|string|null;
publicfunctionread(int$length = 1024): ?string;
publicfunctionwrite(string$data): ?int;
publicfunctionclose(): bool;
publicfunctionisAlive(): bool;
publicfunctiongetSocket(): mixed;
publicfunctiongetChannel(): ChannelInterface;
}

The only implementation in the package is InitPHP\Socket\Server\ServerConnection.

Channel contract

InitPHP\Socket\Interfaces\ChannelInterface

interface ChannelInterface
{
publicfunctionread(int$length = 1024, ?int$flag = null): ?string;
publicfunctionwrite(string$data): ?int;
publicfunctionclose(): bool;
publicfunctionisAlive(): bool;
publicfunctiongetResource(): mixed;
}

Built-in implementations:

ClassBackingNotes
Channel\TcpChannelext-socketsLiveness via `MSG_PEEK
Channel\UdpChannelext-sockets (shared listening socket)Per-peer buffer drained by read(). Exposes feed(), getPeerHost(), getPeerPort(), peerKey().
Channel\StreamChannelstream resourceLiveness via feof().

Enums

See Enums for the full reference.

InitPHP\Socket\Enum\Transport// TCP, UDP, TLS, SSL
InitPHP\Socket\Enum\Domain // V4, V6, UNIXInitPHP\Socket\Enum\CryptoMethod// SSLv2/3/23, ANY, TLS, TLSv1_0/1_1/1_2

Exceptions

See Exceptions for the full reference.

InitPHP\Socket\Exception\SocketExceptionInterface// marker
InitPHP\Socket\Exception\SocketException// \RuntimeException
InitPHP\Socket\Exception\SocketConnectionException// \RuntimeException
InitPHP\Socket\Exception\SocketListenException// \RuntimeException
InitPHP\Socket\Exception\SocketInvalidArgumentException// \InvalidArgumentException

See also

Clone this wiki locally