5. Configuration - swooletw/laravel-swoole GitHub Wiki

Configuration

If you want to change the default configurations, please run the following command to generate configuration files swoole_http.php and swoole_websocket.php in directory /config:

$ php artisan vendor:publish --tag=laravel-swoole

For Lumen users, you need to copy those files to config folder and register them in bootstrap/app.php manually.

swoole_http.php

Key Description
server.host Server listening host address
server.port Server listening port
public_path Public folder path of your project
handle_static_files Determine if to use Swoole to respond request for static files. (You should use Nginx to handle static files instead.)
access_log Determine if to output access logs on the console.
socket_type SWOOLE_SOCK_TCP as default, other types: SWOOLE_SOCK_UDP, SWOOLE_SOCK_TCP6, SWOOLE_SOCK_UDP6, SWOOLE_UNIX_STREAM, SWOOLE_UNIX_DGRAM
server.options The configurations for Swoole\Server. To get more information about swoole server, please read the official documentation
websocket.enabled Determine if to enable websocket server
hot_reload.enabled Determine if to enable hot-reload for develop.
hot_reload.recursively Determine if to scan the directories recursively.
hot_reload.directory The base path for hot-reload.
hot_reload.log Determine if to output the reload status on console.
hot_reload.filter The filter for hot-reload. Default is .php.
ob_output Console output will be transfered to response content if enabled.
pre_resolved Pre-resolved instances here will be resolved when sandbox created.
instances Instances here will be cleared on every request.
providers Service providers here will be re-registered on every request.
tables You are able to define your swoole tables for data sharing cross processes here.

Here are some examples:

[
    'server' => [
        // Options here will pass to Swoole server's configuration directly
        'options' => [
            'max_request' => 1000,
            // You can run your application in deamon
            'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
            // Normally this value should be 1~4 times lager according to your cpu cores 
            'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num() * 2),
            'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num() * 2),
            'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num() * 2),
            // This value should be larger than `post_max_size` and `upload_max_filesize` in `php.ini`.
            // This equals to 10 MB
            'package_max_length' => 10 * 1024 * 1024,
            'buffer_output_size' => 10 * 1024 * 1024,
            // Max buffer size for socket connections
            'socket_buffer_size' => 128 * 1024 * 1024,
            // Worker will restart after processing this number of request
            'max_request' => 3000,
            // Enable coroutine send
            'send_yield' => true,
            // You must add --enable-openssl while compiling Swoole
            'ssl_cert_file' => null,
            'ssl_key_file' => null,
        ],
    ],

    // You can customize your swoole tables here. 
    // See https://wiki.swoole.com/wiki/page/p-table.html for more detailed information.
    'tables' => [
        'table_name' => [
            'size' => 1024,
            'columns' => [
                ['name' => 'column_name', 'type' => Table::TYPE_STRING, 'size' => 1024],
            ]
        ],
    ]
]

swoole_websocket.php

Key Description
handler Websocket handler for onOpen and onClose callback function
parser Default websocket frame parser
route_file Websocket route file path
default Default websocket room driver
middleware Default middleware for on connect request
ping_interval Websocket client's heartbeat interval (ms)
ping_timeout Websocket client's heartbeat interval timeout (ms)
drivers Room drivers mapping
settings Room drivers settings

Here are some examples:

[
    // Replace this handler if you want to customize your websocket handler
    'handler' => SwooleTW\Http\Websocket\SocketIO\WebsocketHandler::class,
    // Replace it if you want to customize your websocket payload
    'parser' => SwooleTW\Http\Websocket\SocketIO\SocketIOParser::class,

    // You can register your websocket event mapping in this route file 
    'route_file' => base_path('routes/websocket.php'),

    // Default middleware for on connect request
    'middleware' => [
        SwooleTW\Http\Websocket\Middleware\DecryptCookies::class,
        SwooleTW\Http\Websocket\Middleware\StartSession::class,
        SwooleTW\Http\Websocket\Middleware\Authenticate::class,
    ],

    // Default room driver, it's `table`(swoole table) by default
    'default' => 'table',

    // Don't forget to add the driver mapping here if you want to use your own driver
    'drivers' => [
        'table' => SwooleTW\Http\Websocket\Rooms\TableRoom::class,
        'redis' => SwooleTW\Http\Websocket\Rooms\RedisRoom::class,
    ],

    // Room driver's settings
    'settings' => [
        // Memory of swoole table is allocated in the very begining of the process.
        // You can't modify it after starting server.
        // So you should set them to proper values
        'table' => [
            'room_rows' => 4096,
            'room_size' => 2048,
            'client_rows' => 8192,
            'client_size' => 2048
        ],
        'redis' => [
            'server' => [
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
                'persistent' => true,
            ],
            'options' => [
                //
            ],
            'prefix' => 'swoole:',
        ]
    ],
]

← Installation | Commands →