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.
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.
To install this package, use Composer:
$ composer require zendframework/zend-expressive-swoole
Swoole with Expressive
zend-expressive-swoole enables an Expressive 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
Expressive skeleton versions prior to 3.1.0
The above will work immediately after installing zend-expressive-swoole if you are using a version of zend-expressive-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/zend-expressive-swoole.local.phpwith the following contents:
<?php use Zend\Expressive\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 [ 'zend-expressive-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 [ 'zend-expressive-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
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
Found a mistake or want to contribute to the documentation? Edit this page on GitHub!