Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

5. Configuration

Albert Chen edited this page May 10, 2018 · 18 revisions

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.
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
ob_output Console output will be transfered to response content if enabled.
instances Instances here will be cleared 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,
        ],
    ],

    // 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
        ],
    ],
]
Clone this wiki locally