| Home | Getting Started | Core Concepts | Helpers | Extensions | Repo |
Swoole is a C-extension HTTP server with coroutine support. Running Tiny under Swoole means the framework boots once and handles thousands of concurrent requests inside lightweight coroutines.
pecl install swoole)The repo ships with a top-level swoole.php:
USE_SWOOLE=1 php swoole.php
By default Swoole listens on port 9501. Set SWOOLE_PORT and SWOOLE_HOST to override.
swoole.php calls tiny::swoole()->start().The application code is identical to PHP-FPM, but a few things behave differently:
die() or exit()Use tiny::die() and tiny::exit() instead. They throw an ExitException that the Swoole worker catches, ending the current coroutine cleanly without killing the whole worker:
// ❌
exit;
// ✅
tiny::exit();
tiny::die('fatal error');
tiny::redirect() (not raw headers)Under Swoole, you can’t header('Location: ...'). tiny::redirect() and $response->redirect() route through Swoole’s response API automatically.
isAsync() returns true$request->isAsync() reports true under Swoole — useful when your views need to behave differently for streamed contexts.
if (tiny::isUsingSwoole()) {
// Swoole-specific tweaks
}
TinyDB detects Swoole and uses coroutine-safe PDO connections. No code change required.
Globals leak between coroutines. Don’t store request-specific state on static class properties or $_SERVER. Use tiny::data(), which is per-request.
tiny::swoole() returns a TinySwoole singleton:
tiny::swoole()->start(); // boot the server
tiny::swoole()->co(fn () => doSomething()); // wrap a callable in a coroutine
tiny::swoole()->header($name, $value); // set a response header
tiny::swoole()->redirect($url, $code); // redirect within Swoole
You’ll rarely need these directly — the framework calls them for you.
In production, run swoole.php under systemd, supervisord, or a container restart policy:
# /etc/systemd/system/myapp.service
[Unit]
Description=My App (Swoole)
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/srv/my-app
Environment=USE_SWOOLE=1
ExecStart=/usr/bin/php /srv/my-app/swoole.php
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target
Then put a reverse proxy (nginx, Caddy) in front to terminate TLS.
Swoole is worth it when you have:
It’s overkill for:
For those, stay on PHP-FPM (optionally with OPcache preloading).