swoole / library
Library of Swoole
Requires
- php: >=8.2
- ext-posix: *
- ext-swoole: >=6.2
- nikic/php-parser: ^5.2
Requires (Dev)
- ext-curl: *
- ext-json: *
- ext-redis: *
- ext-sockets: *
- deminy/counit: ~0.3
- friendsofphp/php-cs-fixer: ^3.0
- mongodb/mongodb: ^2.1
- phpstan/phpstan: ^2.1
- swoole/ide-helper: dev-master
Suggests
- ext-curl: Required to use http client
- ext-mongodb: Required to use mongodb database
- ext-mysqli: Required to use mysqli database
- ext-pdo: Required to use pdo database
- ext-redis: Required to use redis database, and the required version is greater than or equal to 3.1.3
Provides
None
Conflicts
None
Replaces
None
- dev-master
- 6.2.x-dev
- v6.2.6
- v6.2.5
- v6.2.4
- v6.2.3
- v6.2.2
- v6.2.1
- v6.2.0
- 6.1.x-dev
- v6.1.13
- v6.1.12
- v6.1.11
- v6.1.10
- v6.1.9
- v6.1.8
- v6.1.7
- v6.1.6
- v6.1.5
- v6.1.4
- v6.1.3
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.2
- v6.0.1
- v6.0.0
- v6.0.0-rc1
- v6.0.0-beta
- v6.0.0-alpha
- 5.1.x-dev
- v5.1.8
- v5.1.7
- v5.1.6
- v5.1.5
- v5.1.4
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.3
- 4.8.x-dev
- v4.5.x-dev
- v4.4.x-dev
- v0.1.0
- dev-5.1.x-legacy
- dev-futures
- dev-psr
This package is auto-updated.
Last update: 2026-11-17 20:29:39 UTC
README
Dockerized Local Development
First, run one of the following commands to install development packages using Composer:
docker run --rm -v "$(pwd)":/var/www -ti phpswoole/swoole composer update -n # or, use the official Composer Docker image: docker run --rm -v "$(pwd)":/app -ti composer update -n --ignore-platform-reqs # or, use the local Composer if installed: composer update -n --ignore-platform-reqs
Next, you need to start Docker containers:
docker compose up -d
Alternatively, if you need to rebuild some Docker image and to restart the containers:
docker compose build --progress plain --no-cache docker compose up -d --force-recreate
Now you can create a bash session in the app container:
docker compose exec app bash
And run commands inside the container:
composer test
Or you can run commands directly inside the app container:
docker compose exec app composer test
Autoloading and swoole.enable_library
The Swoole extension embeds most of this library in swoole.so, and swoole.enable_library=On is the
default: the extension then already provides classes such as Swoole\ConnectionPool and functions such as
swoole_string(). A few components are Composer-only -- not packed into the extension: FastCGI
(Swoole\FastCGI, Swoole\Coroutine\FastCGI), gRPC (Swoole\Grpc), Kafka (Swoole\Kafka), the debugger
(Swoole\Debugger) and the reverse proxy (Swoole\Proxy).
Install the package (works with either setting)
composer require swoole/library
require 'vendor/autoload.php'; $request = new \Swoole\FastCGI\HttpRequest();
src/vendor_init.php (the only autoload.files entry) makes this work in both modes:
- With
swoole.enable_library=Offit initializes the constant/function files, so the Composer package supplies the whole library. This is how this repository runs its tests and examples (php -d swoole.enable_library=Off your_script.php). - With
swoole.enable_library=Onit detectsSWOOLE_LIBRARYand returns without re-requiring those files (they are already in the extension), so no symbol is declared twice. The Composer PSR-4 rules stay active and supply only the Composer-only components the extension does not pack.
Keep the library and extension versions in sync -- the package version always matches the Swoole extension
version. The build-swoole.yml workflow uses this same composer.json to run the tests against the copy
embedded in the extension (swoole.enable_library=On).
Examples
Once you have Docker containers started (as discussed in previous section), you can use commands like following to run examples under folder examples.
Examples of Database Connection Pool
docker compose exec app php examples/mysqli/base.php docker compose exec app php examples/pdo/base.php docker compose exec app php examples/redis/base.php
Examples of FastCGI Calls
FastCGI is a Composer-only component: \Swoole\FastCGI and \Swoole\Coroutine\FastCGI are loaded through the
Composer autoloader and are not packed into the Swoole extension.
There is a fantastic example showing how to use Swoole as a proxy to serve a WordPress website using PHP-FPM. Just open URL http://127.0.0.1 in the browser and check what you see there. Source code of the example can be found here.
Here are some more examples to make FastCGI calls to PHP-FPM:
docker compose exec app php examples/fastcgi/greeter/call.php docker compose exec app php examples/fastcgi/greeter/client.php docker compose exec app php examples/fastcgi/proxy/base.php docker compose exec app php examples/fastcgi/var/client.php
Third Party Libraries
Here are all the third party libraries used in this project:
- The FastCGI part is derived from Composer package lisachenko/protocol-fcgi.
You can find the licensing information of these third party libraries here.
License
This project follows the Apache 2 license.