Swoole Server

Swoole Server

Swoole Server provides the API to write TCP, UDP, Unix Socket asynchronous servers. It supports IPv4, IPv6, one Way, two Way SSL and TLS Encryption. Developers do not have to know the internal implementations, only have to write the logics of the server in the callback functions.

Swoole server API only can be used in the php CLI mode.


Initialize the server

$server = new Swoole\Server("", 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP);

Set runtime paramaters

    'worker_num' => 4, // The number of worker processes
    'daemonize' => true, // Whether start as a daemon process
    'backlog' => 128, // TCP backlog connection number

Register callback functions

$server->on('Connect', 'my_onConnect');
$server->on('Receive', 'my_onReceive');
$server->on('Close', 'my_onClose');

There are four types of callback functions

Start the server


Get the server attributes

$server->manager_pid; // PID of manager process, send SIGUSR1 to this process to reload the application
$server->master_pid;  // PID of master process, send SIGTERM signal to this process to shutdown the server
$server->connections; // The connections established