Coroutine asynchronous programming framework for PHP

Enable PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, Websocket services in PHP programming language without too much knowledge about non-blocking I/O programming and low-level Linux kernel.

Swoole Coroutine allow developers write async application with coroutine API.

Get started in 10 minutes Tutorial Documentation Community

Compared with other async programming frameworks or softwares such as Nginx, Tornado, Node.js, Swoole has the built-in async, multiple threads I/O modules. Developers can use sync or async, coroutine API to write the applications. Swoole PHP network framework enhances the efficiency of R&D team, enable them to focus on the development of innovative products.

Event-driven, Coroutine, asynchronous programming for PHP

Async TCP / UDP / HTTP / Websocket / HTTP2 client/server side API

IPv4 / IPv6 / Unixsocket / TCP/ UDP and SSL / TLS support

Fast serializer / unserializer

High performance, scalable, support C1000K

Milliseconds task scheduler

Open Source


Get started

  • 1


    Linux users

    pecl install swoole
    Mac users

    brew install php 
    pecl install swoole
  • 2

    HTTP Server

    $http = new swoole_http_server("", 9501);
    $http->on("start", function ($server) {
        echo "Swoole http server is started at\n";
    $http->on("request", function ($request, $response) {
        $response->header("Content-Type", "text/plain");
        $response->end("Hello World\n");
  • 3

    WebSocket Server

    $server = new swoole_websocket_server("", 9502);
    $server->on('open', function($server, $req) {
        echo "connection open: {$req->fd}\n";
    $server->on('message', function($server, $frame) {
        echo "received message: {$frame->data}\n";
        $server->push($frame->fd, json_encode(["hello", "world"]));
    $server->on('close', function($server, $fd) {
        echo "connection close: {$fd}\n";
  • 4

    TCP Server

    $server = new swoole_server("", 9503);
    $server->on('connect', function ($server, $fd){
        echo "connection open: {$fd}\n";
    $server->on('receive', function ($server, $fd, $from_id, $data) {
        $server->send($fd, "Swoole: {$data}");
    $server->on('close', function ($server, $fd) {
        echo "connection close: {$fd}\n";
  • 5

    TCP Client

    $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("", 9501, 0.5);
  • 6

    Async Redis / HTTP / WebSocket Client

    $redis = new Swoole\Redis;
    $redis->connect('', 6379, function ($redis, $result) {
        $redis->set('test_key', 'value', function ($redis, $result) {
            $redis->get('test_key', function ($redis, $result) {
    $cli = new Swoole\Http\Client('', 80);
    $cli->setHeaders(array('User-Agent' => 'swoole-http-client'));
    $cli->setCookies(array('test' => 'value'));
    $cli->post('/dump.php', array("test" => 'abc'), function ($cli) {
        $cli->get('/index.php', function ($cli) {
  • 7


    $server = new swoole_server("", 9502);
    $server->set(array('task_worker_num' => 4));
    $server->on('receive', function($server, $fd, $from_id, $data) {
        $task_id = $server->task("Async");
        echo "Dispath AsyncTask: [id=$task_id]\n";
    $server->on('task', function ($server, $task_id, $from_id, $data) {
        echo "New AsyncTask[id=$task_id]\n";
        $server->finish("$data -> OK");
    $server->on('finish', function ($server, $task_id, $data) {
        echo "AsyncTask[$task_id] finished: {$data}\n";

Use cases

Mobile API server

Internet of Things

Micro services

Web API or web application

Gaming servers

Live chat systems