Event-driven asynchronous & concurrent & coroutine networking engine with high performance for PHP.
C PHP M4 Go C++ CMake
Switch branches/tags
v2.0.7 v2.0.6 v2.0.5 v2.0.4 v2.0.3 v2.0.2-alpha v2.0.1 v1.9.10 v1.9.9 v1.9.8 v1.9.7 v1.9.6 v1.9.5 v1.9.4 v1.9.3-stable v1.9.2-stable v1.9.2-alpha v1.9.1-stable v1.9.1-rc1 v1.9.1-beta v1.9.1-alpha v1.9.0-stable v1.9.0-rc1 v1.9.0-beta v1.9.0-alpha v1.8.14-alpha v1.8.13-stable v1.4.2 v1.4.1 v1.4.0 v1.3.1 v1.3release swoole-1.8.7-beta swoole-1.8.7-alpha swoole-1.8.6-rc3 swoole-1.8.6-rc2 swoole-1.8.6-rc1 swoole-1.8.6-beta swoole-1.8.6-alpha swoole-1.8.5-stable swoole-1.8.5-rc2 swoole-1.8.5-rc1 swoole-1.8.5-beta swoole-1.8.5-alpha swoole-1.8.4-stable swoole-1.8.4-rc1 swoole-1.8.4-beta swoole-1.8.4-alpha swoole-1.8.3-stable swoole-1.8.3-rc2 swoole-1.8.3-rc1 swoole-1.8.3-beta swoole-1.8.3-alpha swoole-1.8.2-stable swoole-1.8.2-rc2 swoole-1.8.2-rc1 swoole-1.8.2-beta swoole-1.8.2-alpha swoole-1.8.1-stable swoole-1.8.1-beta swoole-1.8.1-alpha swoole-1.8.0-stable swoole-1.8.0-rc2 swoole-1.8.0-beta swoole-1.8.0-alpha swoole-1.7.22-stable swoole-1.7.22-rc2 swoole-1.7.22-rc1 swoole-1.7.22-beta swoole-1.7.22-alpha swoole-1.7.21-stable swoole-1.7.21-beta swoole-1.7.21-alpha swoole-1.7.20-stable swoole-1.7.20-beta swoole-1.7.20-alpha swoole-1.7.19-stable swoole-1.7.19-rc2 swoole-1.7.19-rc1 swoole-1.7.19-beta swoole-1.7.19-alpha swoole-1.7.18-stable swoole-1.7.18-rc2 swoole-1.7.18-rc1 swoole-1.7.18-beta swoole-1.7.18-alpha swoole-1.7.17-stable swoole-1.7.17-rc1 swoole-1.7.17-beta swoole-1.7.17-alpha swoole-1.7.16-stable swoole-1.7.16-beta swoole-1.7.16-alpha swoole-1.7.15-stable swoole-1.7.15-rc3 swoole-1.7.15-rc2 swoole-1.7.15-rc1 swoole-1.7.15-beta swoole-1.7.15-alpha swoole-1.7.14-stable
Nothing to show
Latest commit 8d2f555 May 24, 2017 @matyhtf matyhtf revert.
Permalink
Failed to load latest commit information.
benchmark update function arg info. Dec 20, 2016
examples Merge branch '1.9' May 23, 2017
include Merge branch '1.9' May 23, 2017
php-tests fix typo May 23, 2017
src Merge branch '1.9' May 23, 2017
tests fixed unittest code compile error. Feb 17, 2017
thirdparty Compatible with jemalloc May 5, 2017
tools PHPAPI: added macro prefix Apr 20, 2017
travis update file attrs Feb 11, 2017
.gitignore update C++ API. Mar 28, 2017
.gitmodules update package.xml and git submodules. May 4, 2017
.travis.yml Merge branch '2.0' Apr 11, 2017
CMakeLists.txt Added real async resolver support for swoole_async_dns_lookup. Jan 23, 2017
CREDITS some modify.update Apr 11, 2013
LICENSE Update LICENSE Jan 26, 2014
README.md Update README.md Mar 23, 2017
Version2.md Update Version2.md Jan 19, 2017
config.m4 Merge branch '1.9' May 24, 2017
package.xml Merge branch '1.9' May 23, 2017
php7_wrapper.h Merge branch 'master' into 2.0.1 Feb 22, 2017
php_swoole.h update version. May 23, 2017
swoole.c Merge branch '1.9' May 23, 2017
swoole_async.c Merge branch '1.9' Apr 19, 2017
swoole_atomic.c update function arg info. Dec 20, 2016
swoole_buffer.c Added garbage recycling and auto garbage recycling in swoole_buffer. Jan 7, 2017
swoole_channel.c code optimization Mar 10, 2017
swoole_client.c remove swoole_module API. May 19, 2017
swoole_client_coro.c fix sendfile compile error. May 23, 2017
swoole_config.h Merge branch '1.9' May 24, 2017
swoole_coroutine.c merge 2.0 to 2.0-dev Apr 18, 2017
swoole_coroutine.h merge 2.0 Mar 21, 2017
swoole_coroutine_util.c merge 2.0 to 2.0-dev Apr 18, 2017
swoole_event.c don't wait when catch an fatal error on PHP7. Dec 21, 2016
swoole_http.h Limit the number of input variables. Feb 15, 2017
swoole_http_client.c Merge branch '1.9' May 23, 2017
swoole_http_client.h merge. Mar 16, 2017
swoole_http_client_coro.c fix sendfile compile error. May 23, 2017
swoole_http_server.c Merge branch '1.9' May 23, 2017
swoole_http_v2_client.c fix http2 client params parse error May 16, 2017
swoole_http_v2_server.c udpate http2 protocol client and server. Mar 9, 2017
swoole_lock.c update function arg info. Dec 20, 2016
swoole_mmap.c Fix: fd leak Jan 31, 2017
swoole_mysql.c Merge branch '1.9' Apr 19, 2017
swoole_mysql.h Merge branch '2.0' Apr 11, 2017
swoole_mysql_coro.c merge 2.0 Mar 21, 2017
swoole_process.c Merge branch '1.9' Apr 19, 2017
swoole_redis.c Allow to close the connection outside the onConnect callback function. Jan 3, 2017
swoole_redis_coro.c optimize Mar 30, 2017
swoole_redis_server.c fix bug of segmentation fault when item is not a string Apr 24, 2017
swoole_serialize.c revert. May 24, 2017
swoole_serialize.h 优化序列化 May 23, 2017
swoole_server.c Merge branch '1.9' May 23, 2017
swoole_server_port.c Merge branch '1.9' May 23, 2017
swoole_table.c fixed #1154 Mar 24, 2017
swoole_timer.c add dns_coro Mar 24, 2017
swoole_websocket_server.c added coroutine support to onOpen callback. Feb 22, 2017

README.md

Swoole

Build Status License Join the chat at https://gitter.im/swoole/swoole-src Coverity Scan Build Status

Swoole is an event-driven asynchronous & concurrent networking communication framework with high performance written only in C for PHP.

Document: https://rawgit.com/tchiotludo/swoole-ide-helper/english/docs/index.html

IDE Helper: https://github.com/swoole/ide-helper

中文文档: http://wiki.swoole.com/

IRC: https://gitter.im/swoole/swoole-src

event-based

The network layer in Swoole is event-based and takes full advantage of the underlaying epoll/kqueue implementation, making it really easy to serve thousands of connections.

coroutine

Swoole 2.0 supports the built-in coroutine, and you can use fully synchronized code to implement asynchronous programs. PHP code without any additional keywords, the underlying automatic coroutine-scheduling.

<?php
for($i = 0; $i < 100; $i++) {
    Swoole\Coroutine::create(function() use ($i) {
        $redis = new Swoole\Coroutine\Redis();
        $res = $redis->connect('127.0.0.1', 6379);
        $ret = $redis->incr('coroutine');
        $redis->close();
        if ($i == 50) {
            Swoole\Coroutine::create(function() use ($i) {
                $redis = new Swoole\Coroutine\Redis();
                $res = $redis->connect('127.0.0.1', 6379);
                $ret = $redis->set('coroutine_i', 50);
                $redis->close();
            });
        }
    });
}
<?php
$server = new Swoole\Http\Server('127.0.0.1', 9501);

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

    $tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
    $ret = $tcp_cli->connect('127.0.0.1', 9906);
    $tcp_cli ->send('test for the coro');
    $ret = $tcp_cli->recv(5);
    $tcp_cli->close();

    if ($ret) {
        $response->end(" swoole response is ok");
    }
    else{
        $response->end(" recv failed error : {$client->errCode}");
    }
});

$server->start();

concurrent

On the request processing part, Swoole uses a multi-process model. Every process works as a worker. All business logic is executed in workers, synchronously.

With the synchronous logic execution, you can easily write large and robust applications and take advantage of almost all libraries available to the PHP community.

in-memory

Unlike traditional apache/php-fpm stuff, the memory allocated in Swoole will not be freed after a request, which can improve performance a lot.

Why Swoole?

Traditional PHP applications almost always run behind Apache/Nginx, without much control of the request. This brings several limitations:

  1. All memory will be freed after the request. All PHP code needs be re-compiled on every request. Even with opcache enabled, all opcode still needs to be re-executed.
  2. It is almost impossible to implement long connections and connection pooling techniques.
  3. Implementing asynchronous tasks requires 3rd party queue servers, such as rabbitmq and beanstalkd.
  4. Implementing realtime applications such as chatting servers requires 3rd party languages, such as nodejs, for example.

This is why Swoole appeared. Swoole extends the use cases of PHP, and brings all these possibilities to the PHP world. By using Swoole, you can build enhanced web applications with more control, real-time chatting servers, etc, more easily.

Requirements

  • PHP 5.3.10 or later
  • Linux, OS X and basic Windows support (Thanks to cygwin)
  • GCC 4.4 or later

Installation

  1. Install via pecl

    pecl install swoole
    
  2. Install from source

    sudo apt-get install php5-dev
    git clone https://github.com/swoole/swoole-src.git
    cd swoole-src
    phpize
    ./configure
    make && make install
    

Introduction

Swoole includes components for different purposes: Server, Task Worker, Timer, Event and Async IO. With these components, Swoole allows you to build many features.

Server

This is the most important part in Swoole. It provides the necessary infrastructure to build server applications. With Swoole server, you can build web servers, chat messaging servers, game servers and almost anything you want.

The following example shows a simple echo server.

// create a server instance
$serv = new swoole_server("127.0.0.1", 9501);

// attach handler for connect event, once client connected to server the registered handler will be executed
$serv->on('connect', function ($serv, $fd){
    echo "Client:Connect.\n";
});

// attach handler for receive event, every piece of data received by server, the registered handler will be
// executed. And all custom protocol implementation should be located there.
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, $data);
});

$serv->on('close', function ($serv, $fd) {
    echo "Client: Close.\n";
});

// start our server, listen on port and be ready to accept connections
$serv->start();

Try to extend your server and implement what you want!

Http Server

$http = new swoole_http_server("0.0.0.0", 9501);

$http->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

WebSocket Server

$ws = new swoole_websocket_server("0.0.0.0", 9502);

$ws->on('open', function ($ws, $request) {
    var_dump($request->fd, $request->get, $request->server);
    $ws->push($request->fd, "hello, welcome\n");
});

$ws->on('message', function ($ws, $frame) {
    echo "Message: {$frame->data}\n";
    $ws->push($frame->fd, "server: {$frame->data}");
});

$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();

Real async-mysql client

$db = new swoole_mysql('127.0.0.1', 'root', 'root', 'test');

$db->on("close", function($o){
    echo "mysql connection is closed\n";
});

$db->query("select now() as now_t", function($db, $result_rows){
    var_dump($result_rows);
    $db->close();
});

Real async-redis client

$client = new swoole_redis;
$client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) {
    echo "connect\n";
    var_dump($result);
    $client->set('key', 'swoole', function (swoole_redis $client, $result) {
        var_dump($result);
        $client->get('key', function (swoole_redis $client, $result) {
            var_dump($result);
            $client->close();
        });
    });
});

Async http Client

$cli = new swoole_http_client('127.0.0.1', 80);

$cli->setHeaders(['User-Agent' => "swoole"]);
$cli->post('/dump.php', array("test" => '9999999'), function (swoole_http_client $cli)
{
    echo "#{$cli->sock}\tPOST response Length: " . strlen($cli->body) . "\n";
    $cli->get('/index.php', function (swoole_http_client $cli)
    {
        echo "#{$cli->sock}\tGET response Length: " . strlen($cli->body) . "\n";
    });
});

Async WebSocket Client

$cli = new swoole_http_client('127.0.0.1', 9501);

$cli->on('message', function ($_cli, $frame) {
    var_dump($frame);
});

$cli->upgrade('/', function ($cli) {
    echo $cli->body;
    $cli->push("hello world");
});

Multi-port and mixed protocol

$serv = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE);

$port2 = $serv->listen("0.0.0.0", 9502, SWOOLE_SOCK_TCP);
$port2->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
    var_dump($data);
    $serv->send($fd, $data);
});

$serv->on('request', function($req, $resp) {
    $resp->end("<h1>Hello World</h1>");
});


$serv->start();

Task Worker

Swoole brings you two types of workers: server workers and task workers. Server workers are for request handling, as demonstrated above. Task workers are for task execution. With task workers, we can execute our task asynchronously without blocking the server workers.

Task workers are mainly used for time-consuming tasks, such as sending password recovery emails. And ensure the main request returns as soon as possible.

The following example shows a simple server with task support.

$serv = new swoole_server("127.0.0.1", 9502);

// sets server configuration, we set task_worker_num config greater than 0 to enable task workers support
$serv->set(array('task_worker_num' => 4));

// attach handler for receive event, which have explained above.
$serv->on('receive', function($serv, $fd, $from_id, $data) {
    // we dispath a task to task workers by invoke the task() method of $serv
    // this method returns a task id as the identity of ths task
    $task_id = $serv->task($data);
    echo "Dispath AsyncTask: id=$task_id\n";
});

// attach handler for task event, the handler will be executed in task workers.
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
    // handle the task, do what you want with $data
    echo "New AsyncTask[id=$task_id]".PHP_EOL;

    // after the task task is handled, we return the results to caller worker.
    $serv->finish("$data -> OK");
});

// attach handler for finish event, the handler will be executed in server workers, the same worker dispatched this task before.
$serv->on('finish', function ($serv, $task_id, $data) {
    echo "AsyncTask[$task_id] Finish: $data".PHP_EOL;
});

$serv->start();

Swoole also supports synchronous tasks. To use synchronous tasks, just simply replace $serv->task($data) with $serv->taskwait($data). Unlike task(), taskwait() will wait for a task to complete before it returns its response.

Timer

Swoole has built in millisecond timer support. By using the timer, it is easy to get a block of code executed periodically (really useful for managing interval tasks).

To demonstrate how the timer works, here is a small example:

//interval 2000ms
$serv->tick(2000, function ($timer_id) {
    echo "tick-2000ms\n";
});

//after 3000ms
$serv->after(3000, function () {
    echo "after 3000ms.\n"
});

In the example above, we first set the timer event handler to swoole_server to enable timer support. Then, we add two timers by calling bool swoole_server::addtimer($interval) once the server started. To handle multiple timers, we switch the $interval in registered handler and do what we want to do.

Event

Swoole's I/O layer is event-based, which is very convenient to add your own file descriptor to Swoole's main eventloop. With event support, you can also build fully asynchronous applications with Swoole.

To use events in Swoole, we can use swoole_event_set() to register event handler to sepecified file descriptor, once registered descriptors become readable or writeable, our registered handler will be invoked. Also, we can using bool swoole_event_del(int $fd); to remove registered file descriptor from eventloop.

The following are prototypes for the related functions:

bool swoole_event_add($fd, mixed $read_callback, mixed $write_callback, int $flag);
bool swoole_event_set($fd, mixed $read_callback, mixed $write_callback, int $flag);
bool swoole_event_del($fd);

The $fid parameter can be one of the following types:

  • unix file descriptor
  • stream resource created by stream_socket_client()/fsockopen()
  • sockets resources created by socket_create() in sockets extension (require compile swoole with --enable-sockets support)

The $read_callback and $write_callback are callbacks for corresponding read/write event.

The $flag is a mask to indicate what type of events we should get notified, can be SWOOLE_EVENT_READ, SWOOLE_EVENT_WRITE or SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE

Async IO

Swoole's Async IO provides the ability to read/write files and lookup dns records asynchronously. The following are signatures for these functions:

bool swoole_async_readfile(string $filename, mixed $callback);
bool swoole_async_writefile('test.log', $file_content, mixed $callback);
bool swoole_async_read(string $filename, mixed $callback, int $trunk_size = 8192);
bool swoole_async_write(string $filename, string $content, int $offset = -1, mixed $callback = NULL);
void swoole_async_dns_lookup(string $domain, function($host, $ip){});
bool swoole_timer_after($after_n_ms, mixed $callback);
bool swoole_timer_tick($n_ms, mixed $callback);
bool swoole_timer_clear($n_ms, mixed $callback);

Refer to API Reference for more detailed information about these functions.

Client

Swoole also provides a Client component to build tcp/udp clients in both asynchronous and synchronous ways. Swoole uses the swoole_client class to expose all its functionalities.

synchronous blocking:

$client = new swoole_client(SWOOLE_SOCK_TCP);
if (!$client->connect('127.0.0.1', 9501, 0.5))
{
    die("connect failed.");
}

if (!$client->send("hello world"))
{
    die("send failed.");
}

$data = $client->recv();
if (!$data)
{
    die("recv failed.");
}

$client->close();

asynchronous nonblocking:

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

$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";
});

$client->connect('127.0.0.1', 9501, 0.5);

The following methods are available in swoole_client:

swoole_client::__construct(int $sock_type, int $is_sync = SWOOLE_SOCK_SYNC, string $key);
int swoole_client::on(string $event, mixed $callback);
bool swoole_client::connect(string $host, int $port, float $timeout = 0.1, int $flag = 0)
bool swoole_client::isConnected();
int swoole_client::send(string $data);
bool swoole_client::sendfile(string $filename)
string swoole_client::recv(int $size = 65535, bool $waitall = 0);
bool swoole_client::close();

Refer to API Reference for more detailed information about these functions.

API Reference

Related Projects

Contribution

Your contribution to Swoole development is very welcome!

You may contribute in the following ways:

License

Apache License Version 2.0 see http://www.apache.org/licenses/LICENSE-2.0.html