We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Micro + Swoole HTTP Server

Hi, I just create an environment that running Phalcon on top of Swoole. I have a script that I upload on Github Gist, here is my script: https://gist.github.com/maulanasatyaadi/552fbd54ff7e23b3b8ffb6adf1562a6f

I have to test with a heavy compute request, and the result has good enough. On AMD A9 dual-core processor with each 3.6GHz (turbo on), it can reach 3532 requests per second (Swoole HTTP) with 0 error, 5922 requests per second (Swoole on Unix socket + Nginx) with 0 error, and with Nginx + PHP-FPM it just reach 1385 requests per second. And of course, there is no memory leak, I have tested it too.

Here is my code using Nginx + PHP-FPM:

<?php
use Swoole\Http\Server;
use Phalcon\Mvc\Micro;
use Phalcon\Di\FactoryDefault;
// Load factory default dependencies
$di = new FactoryDefault();
// Load application based on dependency injection
$app = new Micro($di);
// Set response while path not found
$app->notFound(function() {
    return ['not found'];
});
// Set response for path '/'. This is default response (like index.php)
$app->get('/', function() {
    return ['Hello world!'];
});
echo json_encode(['message' => $app->handle()]);

My bottleneck is I don't know how to make an HTTP request adapter in Phalcon that maybe make this method better. I hope in the future someone makes an HTTP request adapter that supports the Swoole HTTP request interface.

edited Nov '19

you can try manaphp[https://github.com/manaphp/manaphp] framework ,it suports fpm and swoole coroutine.

you can try manaphp[https://github.com/manaphp/manaphp] framework ,it suports fpm and swoole coroutine.

But the documentation is Chinese. I can't read that.



133

MVC implementation index.php:

declare(strict_types=1);

use Swoole\Http\Server;
use Phalcon\Mvc\Application;

error_reporting(E_ALL);

try {
        /**
     * Read services
     */
    require CONFIG_PATH . 'services.php';
    /**
     * Get config service for use in inline setup below
     */
    $config = $di->getConfig();
        /**
        * Include Autoloader
        */
        include CONFIG_PATH . 'loader.php';
        /**
        * Initialize http server
        */
    $http = new Server('0.0.0.0', 9501); 

        $http->on('WorkerStart', function($server, $workerId) use($di) { 
            $application = new Application($di);
        $server->application = $application;
    });

        $http->on('request', function ($request, $response) use($di) { 

            $_GET = $_POST = $_COOKIE = $_REQUEST = [];

            switch($request) {
                case !empty($request->get):
                    $_GET = $request->get;
                    $_REQUEST += $_GET;
                break;
                case !empty($request->post):
                    $_POST = $request->post;
                    $_REQUEST += $_POST;
                break;
                case !empty($request->cookie):
                    $_COOKIE = $request->cookie;
                break;
            }

            $application = new Application($di);

            $html = $application->handle($request->server['request_uri'])->getContent();

            $response->end($html);
        });
        /**
     * Start application server
     */
        $http->start();

}  catch (\Exception $e) {
    echo $e->getMessage() . '<br>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

Start server at command prompt in your public folder: php index.php Open app on browser: https://your_ip:9501 https://your_ip:9501/controller/action/../../..

you can try manaphp[https://github.com/manaphp/manaphp] framework ,it suports fpm and swoole coroutine. 垃圾东西四处丢人