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:

  1. $ 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

KeyDescription
server.hostServer listening host address
server.portServer listening port
public_pathPublic folder path of your project
handle_static_filesDetermine if to use Swoole to respond request for static files. (You should use Nginx to handle static files instead.)
access_logDetermine if to output access logs on the console.
socket_typeSWOOLE_SOCK_TCP as default, other types: SWOOLE_SOCK_UDP, SWOOLE_SOCK_TCP6, SWOOLE_SOCK_UDP6, SWOOLE_UNIX_STREAM, SWOOLE_UNIX_DGRAM
server.optionsThe configurations for Swoole\Server. To get more information about swoole server, please read the official documentation
websocket.enabledDetermine if to enable websocket server
hot_reload.enabledDetermine if to enable hot-reload for develop.
hot_reload.recursivelyDetermine if to scan the directories recursively.
hot_reload.directoryThe base path for hot-reload.
hot_reload.logDetermine if to output the reload status on console.
hot_reload.filterThe filter for hot-reload. Default is .php.
ob_outputConsole output will be transfered to response content if enabled.
pre_resolvedPre-resolved instances here will be resolved when sandbox created.
instancesInstances here will be cleared on every request.
providersService providers here will be re-registered on every request.
tablesYou are able to define your swoole tables for data sharing cross processes here.

Here are some examples:

  1. [
  2. 'server' => [
  3. // Options here will pass to Swoole server's configuration directly
  4. 'options' => [
  5. 'max_request' => 1000,
  6. // You can run your application in deamon
  7. 'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
  8. // Normally this value should be 1~4 times lager according to your cpu cores
  9. 'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num() * 2),
  10. 'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num() * 2),
  11. 'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num() * 2),
  12. // This value should be larger than `post_max_size` and `upload_max_filesize` in `php.ini`.
  13. // This equals to 10 MB
  14. 'package_max_length' => 10 * 1024 * 1024,
  15. 'buffer_output_size' => 10 * 1024 * 1024,
  16. // Max buffer size for socket connections
  17. 'socket_buffer_size' => 128 * 1024 * 1024,
  18. // Worker will restart after processing this number of request
  19. 'max_request' => 3000,
  20. // Enable coroutine send
  21. 'send_yield' => true,
  22. // You must add --enable-openssl while compiling Swoole
  23. 'ssl_cert_file' => null,
  24. 'ssl_key_file' => null,
  25. ],
  26. ],
  27.  
  28. // You can customize your swoole tables here.
  29. // See https://wiki.swoole.com/wiki/page/p-table.html for more detailed information.
  30. 'tables' => [
  31. 'table_name' => [
  32. 'size' => 1024,
  33. 'columns' => [
  34. ['name' => 'column_name', 'type' => Table::TYPE_STRING, 'size' => 1024],
  35. ]
  36. ],
  37. ]
  38. ]

swoole_websocket.php

KeyDescription
handlerWebsocket handler for onOpen and onClose callback function
parserDefault websocket frame parser
route_fileWebsocket route file path
defaultDefault websocket room driver
middlewareDefault middleware for on connect request
ping_intervalWebsocket client's heartbeat interval (ms)
ping_timeoutWebsocket client's heartbeat interval timeout (ms)
driversRoom drivers mapping
settingsRoom drivers settings

Here are some examples:

  1. [
  2. // Replace this handler if you want to customize your websocket handler
  3. 'handler' => SwooleTW\Http\Websocket\SocketIO\WebsocketHandler::class,
  4. // Replace it if you want to customize your websocket payload
  5. 'parser' => SwooleTW\Http\Websocket\SocketIO\SocketIOParser::class,
  6.  
  7. // You can register your websocket event mapping in this route file
  8. 'route_file' => base_path('routes/websocket.php'),
  9.  
  10. // Default middleware for on connect request
  11. 'middleware' => [
  12. SwooleTW\Http\Websocket\Middleware\DecryptCookies::class,
  13. SwooleTW\Http\Websocket\Middleware\StartSession::class,
  14. SwooleTW\Http\Websocket\Middleware\Authenticate::class,
  15. ],
  16.  
  17. // Default room driver, it's `table`(swoole table) by default
  18. 'default' => 'table',
  19.  
  20. // Don't forget to add the driver mapping here if you want to use your own driver
  21. 'drivers' => [
  22. 'table' => SwooleTW\Http\Websocket\Rooms\TableRoom::class,
  23. 'redis' => SwooleTW\Http\Websocket\Rooms\RedisRoom::class,
  24. ],
  25.  
  26. // Room driver's settings
  27. 'settings' => [
  28. // Memory of swoole table is allocated in the very begining of the process.
  29. // You can't modify it after starting server.
  30. // So you should set them to proper values
  31. 'table' => [
  32. 'room_rows' => 4096,
  33. 'room_size' => 2048,
  34. 'client_rows' => 8192,
  35. 'client_size' => 2048
  36. ],
  37. 'redis' => [
  38. 'server' => [
  39. 'host' => env('REDIS_HOST', '127.0.0.1'),
  40. 'password' => env('REDIS_PASSWORD', null),
  41. 'port' => env('REDIS_PORT', 6379),
  42. 'database' => 0,
  43. 'persistent' => true,
  44. ],
  45. 'options' => [
  46. //
  47. ],
  48. 'prefix' => 'swoole:',
  49. ]
  50. ],
  51. ]