Swoole is a PHP extension that provides an event loop, coroutines, and protocol servers. It targets HTTP APIs, WebSockets, and long-lived TCP or UDP services.
Overview
Swoole combines a C and C++ core with a PHP library to run persistent network programs and concurrent I/O in PHP. The reactor watches sockets and dispatches events while worker processes can scale across CPU cores; coroutines let other work proceed while one task waits on supported I/O.
Server APIs cover generic TCP, UDP, and Unix sockets, dedicated HTTP and WebSocket servers, and coroutine-managed HTTP serving. Clients include coroutine TCP, UDP, HTTP, and lower-level socket APIs, plus optional hooks that make some blocking PHP functions yield inside coroutines.
HTTP support spans request and response handling, uploads, streaming output, HTTP/2, and WebSocket framing. Connection pools in the PHP layer help reuse database and Redis connections; process tools such as Task workers, Timer, Table shared memory, and atomics coordinate background work and cross-process state.
Install through PECL or by compiling the extension, then load it in php.ini. Typical first steps run an HTTP server script that stays alive until stopped. Optional build flags add TLS, curl, PostgreSQL, DNS via c-ares, io_uring on Linux, and thread support with a ZTS PHP build.
Key features
- Event-driven server and client APIs
- PHP coroutines with Channel coordination
- HTTP, HTTP/2, and WebSocket servers
- Coroutine HTTP and TCP clients
- Runtime hooks for blocking PHP calls
- Task workers and shared memory Table
- Connection pools for databases and Redis
- Multi-worker and timer scheduling
Use cases
- Long-running PHP HTTP microservices
- WebSocket chat or realtime backends
- High-connection-count TCP gateways
- Concurrent upstream HTTP or RPC calls