An extension for Utopia Framework to work with PHP Swoole as a PHP FPM alternative

0.7.0 2022-11-02 10:32 UTC


Build Status Discord Total Downloads

An extension for Utopia Framework to work with PHP Swoole as a PHP FPM alternative. This library extends the default Utopia Request & Response classes with ones that can interact with the Swoole server. This library is aiming to be as simple and easy to learn and use. This library is maintained by the Appwrite team.

This library is part of the Utopia Framework project. Your PHP installation should be compiled with the PHP Swoole extension for this library to work with Utopia PHP.

Getting Started

Install using composer:

composer require utopia-php/swoole

if (file_exists(__DIR__.'/../vendor/autoload.php')) {
    require __DIR__.'/../vendor/autoload.php';

use Utopia\App;
use Utopia\Swoole\Request;
use Utopia\Swoole\Response;
use Utopia\Swoole\Files;
use Swoole\Http\Server;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;

$http = new Server("", 80);

Files::load(__DIR__ . '/../public'); // Static files location

$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
    $request = new Request($swooleRequest);
    $response = new Response($swooleResponse);

    if(Files::isFileLoaded($request->getURI())) { // output static files with cache headers
        $time = (60 * 60 * 24 * 365 * 2); // 45 days cache

            ->addHeader('Cache-Control', 'public, max-age='.$time)
            ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $time).' GMT') // 45 days cache


    $app = new App('Asia/Tel_Aviv');
    try {
        $app->run($request, $response);
    } catch (\Throwable $th) {
        $swooleResponse->end('500: Server Error');


System Requirements

Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.

Copyright and license

The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php