Swoole

Swoole is a PECL extension for developing asynchronous applications in PHP. It enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, or Websocket services without requiring in-depth knowledge about non-blocking I/O programming or the low-level Linux kernel.

Install swoole

You can install the Swoole extension on Linux or Mac environments using the following commands:

$ pecl install swoole

For more information on the extension, visit its package details on PECL.

Install mezzio-swoole

To install this package, use Composer:

$ composer require mezzio/mezzio-swoole

Swoole with Mezzio

mezzio-swoole enables an Mezzio application to be executed with the Swoole extension. This means you can run the application from the command line, without requiring a web server.

You can run the application using the following command:

$ php public/index.php

This command will execute Swoole on localhost via port 8080.

Mezzio skeleton versions prior to 3.1.0

The above will work immediately after installing mezzio-swoole if you are using a version of mezzio-skeleton from 3.1.0 or later.

For applications based on previous versions of the skeleton, you will need to create a configuration file such as config/autoload/mezzio-swoole.global.php or config/autoload/mezzio-swoole.local.php with the following contents:

<?php
use Mezzio\Swoole\ConfigProvider;

return (new ConfigProvider())();

You can change the host address and/or host name as well as the port using a configuration file, as follows:

// In config/autoload/swoole.local.php:
return [
    'mezzio-swoole' => [
        'swoole-http-server' => [
            'host' => '192.168.0.1',
            'port' => 9501,
        ],
    ],
];

Providing additional Swoole configuration

You can also configure the Swoole HTTP server using an options key to specify any accepted Swoole settings. For instance, the following configuration demonstrates enabling SSL:

// config/autoload/swoole.local.php
return [
    'mezzio-swoole' => [
        'swoole-http-server' => [
            'host' => '192.168.0.1',
            'port' => 9501,
            'mode' => SWOOLE_BASE, // SWOOLE_BASE or SWOOLE_PROCESS;
                                   // SWOOLE_BASE is the default
            'protocol' => SWOOLE_SOCK_TCP | SWOOLE_SSL, // SSL-enable the server
            'options' => [
                // Set the SSL certificate and key paths for SSL support:
                'ssl_cert_file' => 'path/to/ssl.crt',
                'ssl_key_file' => 'path/to/ssl.key',
                // Available in Swoole 4.1 and up; enables coroutine support
                // for most I/O operations:
                'enable_coroutine' => true,
            ],
        ],
    ],
];

Serving static files

We support serving static files. By default, we serve files with extensions in the whitelist defined in the constant Mezzio\Swoole\StaticResourceHandler\ContentTypeFilterMiddleware::DEFAULT_STATIC_EXTS, which is derived from a list of common web MIME types maintained by Mozilla. Our static resource capabilities are fairly comprehensive; please see the chapter on static resources for full details on configuration.