-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Muhammet Şafak edited this page May 24, 2026
·
2 revisions
Welcome to the official documentation for initphp/socket — a lightweight TCP, UDP, TLS and SSL socket server / client toolkit for PHP 8.1+.
composer require initphp/socketuse InitPHP\Socket\Socket;
use InitPHP\Socket\Enum\Transport;
$server = Socket::server(Transport::TCP, '127.0.0.1', 8080);
$server->listen();
$server->live(function ($srv, $conn) {
$payload = $conn->read(1024);
if ($payload !== null) {
$conn->write("echo: {$payload}");
}
});The package gives you four building blocks every long-running PHP service ends up writing by hand:
| Layer | Class / Concept | Purpose |
|---|---|---|
| Factory |
Socket::server() / Socket::client()
|
One entry point dispatching by Transport enum |
| Servers | Server\{TCP, UDP, TLS, SSL} |
Bind, listen and dispatch with a select-driven loop |
| Clients | Client\{TCP, UDP, TLS, SSL} |
Connect-read-write to a remote endpoint |
| Channels | Channel\{Tcp, Udp, Stream}Channel |
Per-transport I/O strategy used by ServerConnection
|
- Installation — composer, requirements, optional extensions.
- Quick Start — bind, accept, echo in under 20 lines.
- Architecture — how the factory, channels, loop and connections fit together.
-
Transport TCP — stream-oriented sockets,
ext-socketsbackend. - Transport UDP — datagram sockets with per-peer routing.
-
Transport TLS —
tls://streams, modern handshake. -
Transport SSL —
ssl://streams, legacy fallback.
- Server Lifecycle · Client Lifecycle — what each method does and when.
- Connection and Channel — the per-accepted-client object and its strategy.
-
Event Loop Integration — embedding the server in your own loop with
tick(). - API Reference — single-page method matrix.
- Enums · Exceptions · SSL Context Options
- Recipe Echo Server — minimal TCP / UDP echo.
- Recipe Chat Server — registered clients + targeted broadcast.
-
Recipe SMTP Client — raw
EHLOover implicit TLS. - Recipe Broadcast — fan-out to all / by-id / by-list.
- Recipe Self Signed TLS — generate a cert, bind a TLS server, connect from a client.
- Recipe Custom Channel — write your own transport strategy.
- Testing Strategy — unit + integration patterns we use ourselves.
- Migration Guide — moving from 1.x to 2.x.
- FAQ — common questions and gotchas.
initphp/socket · MIT · PHP 8.1+ · part of the InitPHP family · file issues at InitPHP/Socket/issues
Getting started
Transports
Concepts
Reference
Recipes
Operational