owner888 / php-async
Zero-dependency pure-PHP Fiber runtime with pluggable scheduling, native I/O polling, non-blocking TCP/TLS sockets, DNS, futures and cancellation.
Requires
- php: >=8.3
- ext-openssl: *
Requires (Dev)
- phpunit/phpunit: ^11.0 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A dependency-free Fiber runtime for PHP 8.3+: non-blocking TCP/TLS sockets, asynchronous DNS, futures, cancellation, and a pluggable event-loop scheduler. It contains no HTTP, SMTP or IMAP protocol logic.
The shared runtime is maintained in pig and used by MyPHP and php-http. It needs PHP's standard ext-openssl, but no Composer runtime dependency or event-loop extension.
Installation
composer require owner888/php-async
Standalone runtime
use Owner888\Async\Async; use Owner888\Async\Socket; Async::run(static function (): void { // Connect to a local TCP service that is already running. $socket = Socket::connect('127.0.0.1', 9000, timeout: 5.0); try { $socket->write("hello\n", 5.0); echo $socket->read(8192, 5.0); } finally { $socket->close(); } });
Async::spawn() starts another Fiber and returns a Future; Future::await() suspends the caller until completion. AbortController provides cancellation, Mutex serializes Fiber critical sections, and Channel provides Go-style unbuffered/buffered Fiber channels. The default Loop uses native Io\Poll on PHP 8.6+ where supported, and falls back to stream_select otherwise.
Embed in an existing event loop
Install a Scheduler before any asynchronous work. It owns readiness watchers, timers and deferred callbacks directly; the runtime does not create a second poller.
For example, with an already-installed Revolt event loop (Revolt is not a dependency of php-async):
use Owner888\Async\{Loop, Scheduler}; use Revolt\EventLoop; use Revolt\EventLoop\Driver; Loop::useScheduler(new class(EventLoop::getDriver()) implements Scheduler { public function __construct(private Driver $driver) {} public function defer(Closure $callback): void { $this->driver->queue($callback); } public function delay(float $seconds, Closure $callback): string { return $this->driver->delay($seconds, static fn (string $id) => $callback()); } public function onReadable($stream, Closure $callback): string { return $this->driver->onReadable($stream, static fn (string $id, $ready) => $callback($ready)); } public function onWritable($stream, Closure $callback): string { return $this->driver->onWritable($stream, static fn (string $id, $ready) => $callback($ready)); } public function cancel(string $id): void { $this->driver->cancel($id); } });
Call sockets and Future::await() inside a host Fiber. Let the host drive its loop. Loop::get() and Async::run() are native-loop-only APIs and reject an external scheduler. Scheduler selection is immutable for the lifetime of normal asynchronous work. In prefork services, install a worker-local scheduler before opening sockets and do not inherit live watchers or connections from the master.
If a GUI owns the main thread but cannot supply portable readiness watchers, its native timer may drive Loop::get()->turn(), a non-blocking iteration of the default loop.
Development
composer install
composer test
The standalone sources are synchronized from pig by MyPHP's scripts/sync-net.php. Protocol-specific HTTP code belongs in owner888/php-http, not here. CI covers PHP 8.3–8.6 on Linux, plus PHP 8.6 on macOS and Windows.
License
MIT