Download the PHP package initphp/socket without Composer
On this page you can find all versions of the php package initphp/socket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package socket
initphp/socket
A lightweight TCP, UDP, TLS and SSL socket toolkit for PHP 8.1+. Both server
and client sides share a clean, typed API built around enums and a small
Channel strategy so each transport plugs in without switch ladders.
Requirements
- PHP 8.1+
- ext-sockets
- ext-openssl (TLS / SSL)
- ext-pcntl (only for the integration test suite)
Installation
Features
- TCP, UDP, TLS, SSL — one factory, one interface per side.
- Non-blocking, select-driven server loop —
live()runs forever, or drive the loop yourself one iteration at a time withtick()(great for embedding into your own event loop or for deterministic tests). - First-class enums —
Transport,DomainandCryptoMethodreplace magic strings and integer flags. - Strategy-based channels —
TcpChannel,UdpChannelandStreamChannelisolate the transport-specific I/O. No static state shared between server instances. - Coherent exception hierarchy — every exception implements
SocketExceptionInterface, so a single catch covers the package. - Typed everywhere — PHP 8.1 enums, readonly promoted properties, full
declare(strict_types=1)coverage. - No silent data loss — liveness checks never consume data from the wire.
Quick start
TCP echo server
TCP client
TLS server (chat-style with named clients)
SSL client (talking to Gmail SMTP)
API surface
Factory — InitPHP\Socket\Socket
Servers — InitPHP\Socket\Interfaces\SocketServerInterface
| Method | Purpose |
|---|---|
listen(): static |
Bind and start listening. Does not accept clients. |
live(callable $cb, float $idle = 0.05): void |
Run the accept/dispatch loop until stop() is called. |
tick(callable $cb, float $wait = 0.0): int |
One iteration of the loop. Returns events processed. |
stop(): void |
Cooperatively exit the loop started by live(). |
close(): bool |
Tear everything down (every client + the listening socket). |
broadcast(string $msg, int\|string\|array\|null $ids = null): bool |
Send to all clients or a targeted subset. |
register(int\|string $id, SocketConnectionInterface $conn): bool |
Attach an addressable id to a connection. |
getClients(): array |
Map of id|key → connection. |
AbstractStreamServer (TLS / SSL) additionally exposes:
Clients — InitPHP\Socket\Interfaces\SocketClientInterface
| Method | Purpose |
|---|---|
connect(): static |
Open the connection. |
disconnect(): bool |
Close the connection. Idempotent. |
read(int $len = 1024): ?string |
Receive up to $len bytes. Returns null on no data / failure. |
write(string $data): ?int |
Send $data. Returns the number of bytes written, or null on failure. |
AbstractStreamClient (TLS / SSL) adds option(), timeout(), blocking()
and crypto() — same shape as the server side.
Enums
Exceptions
Every package exception implements SocketExceptionInterface, so a single
catch (SocketExceptionInterface $e) covers them all.
Embedding into your own event loop
If you already run an event loop (ReactPHP, Amp, Swoole-bridge, etc.), do
not call live() — invoke tick() from your loop and let the host decide
when to yield:
Documentation
In-depth guides live under docs/:
- Getting started
- Architecture
- Servers: SSL
- Clients: SSL
- Cookbook: SMTP client
- Migrating from 1.x
Development
CI runs the full QA pipeline on PHP 8.1, 8.2 and 8.3.
Contributing
Issues, ideas and pull requests are welcome. Please read the org-wide contributing guide before opening a PR.
Security issues should be reported privately — see SECURITY.md.
Credits
License
Released under the MIT License.
All versions of socket with dependencies
ext-sockets Version *
ext-openssl Version *