Fork me on GitHub

Swoole:Redefined PHP Program Language

Swoole is an event-driven, asynchronous & concurrent networking communication engine with higher performance written only in C for PHP. Swoole includes components for different purposes: TCP/UDP Server and Client, Task Worker, Database Connection Pooling, Millisecond Timer, Event, Async IO, Async Http/WebSocket Client, Async Redis Client, Async MySQL Client, and Async DNS Requiring. With these components, Swoole allows you to build many features, which are widely used in the applications about Internet, mobile communication, enterprise software, cloud computing, online game, IoT, IoV, and smart home, etc. For IT development group, it can improve the efficiency, which makes them focus on the innovational products.

  Download (1.8.11)  



Now, Swoole has been utilized by a number of applications like mobile Internet, networking communication, online games, mobile games and enterprises, instead of using C++ or Java to develop network program.  By using PHP + Swoole, development efficiency can be improved greatly. Our well-known some large customers in China, but not limited, are: Tencent, Alibaba, Baidu, YY, etc. The PHP network programming framework is provided officially based on Swoole extension, which can support for Http, FastCGI, WebSocket, FTP, SMTP, SOA and other network protocols.


$serv = new swoole_http_server("", 9502);

$serv->on('Request', function($request, $response) {

    $response->cookie("User", "Swoole");
    $response->header("X-Server", "Swoole");

Hello Swoole!

"); }); $serv->start();

WebSocket Server

$serv = new swoole_websocket_server("", 9502);

$serv->on('Open', function($server, $req) {
    echo "connection open: ".$req->fd;

$serv->on('Message', function($server, $frame) {
    echo "message: ".$frame->data;
    $server->push($frame->fd, json_encode(["hello", "world"]));

$serv->on('Close', function($server, $fd) {
    echo "connection close: ".$fd;


TCP Server

$serv = new swoole_server("", 9501);
    'worker_num' => 8,
    'daemonize' => true,
$serv->on('connect', function ($serv, $fd){
    echo "Client:Connect.\n";
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, 'Swoole: '.$data);
$serv->on('close', function ($serv, $fd) {
    echo "Client: Close.\n";

TCP Client

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
//set event callback
$client->on("connect", function($cli) {
    $cli->send("hello world\n");
$client->on("receive", function($cli, $data){
    echo "Received: ".$data."\n";
$client->on("error", function($cli){
    echo "Connect failed\n";
$client->on("close", function($cli){
    echo "Connection close\n";
//connect to server
$client->connect('', 9501, 0.5);


$fp = stream_socket_client("tcp://", $code, $msg, 3);
$http_request = "GET /index.html HTTP/1.1\r\n\r\n";
fwrite($fp, $http_request);
swoole_event_add($fp, function($fp){
    echo fread($fp, 8192);
swoole_timer_after(2000, function() {
    echo "2000ms timeout\n";
swoole_timer_tick(1000, function() {
    echo "1000ms interval\n";


$serv = new swoole_server("", 9502);
$serv->set(array('task_worker_num' => 4));
$serv->on('Receive', function($serv, $fd, $from_id, $data) {
    $task_id = $serv->task("Async");
    echo "Dispath AsyncTask: id=$task_id\n";
$serv->on('Task', function ($serv, $task_id, $from_id, $data) {
    echo "New AsyncTask[id=$task_id]".PHP_EOL;
    $serv->finish("$data -> OK");
$serv->on('Finish', function ($serv, $task_id, $data) {
    echo "AsyncTask[$task_id] Finish: $data".PHP_EOL;