Coroutine HTTP Server

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Experimental feature, not recommended for production use

Swoole Coroutine HTTP Server which uses the HTTP protocol and allows you to create a server within a coroutine context programmatically on the fly. It is a HTTP server with a focus on utilising coroutines.

Multiple Coroutine HTTP Servers can be created within one process. Servers can be created and destroyed dynamically at runtime or throughout a script.

This HTTP coroutine focused server is different and not a replacement of the Swoole\Server or Swoole\Http\Server classes that use event callbacks. This coroutine HTTP server can be used with Swoole\Process\Pool to manage its server events in a certain order like Connect, Receive and Close.

The implementation for this server is purely C++ and it is not a sub-class of the Swoole\Coroutine\Server, because of this performance is very fast and efficient.

If you have enabled HTTP2 support via --enable-http2 then by default, this server will turn on support for HTTP2 processing, there is no need to set this within its configuration like you have to with the normal Swoole HTTP Server. When using HTTP2 please always keep your Swoole version up-to-date for bug fixes and support.

The full namespace for this class is Swoole\Coroutine\Http\Server but it also has a shorthand alias as well, which is Co\Http\Server.

First available since version v4.4

Please use the normal Swoole TCP/UDP or HTTP Server if you have no special requirements

Methods


Quick Start Example

Simple URL Pattern Matching

<?php

use Swoole\Coroutine\Http\Server;

// Create main coroutine context
Co\run(function()
{
    // Create a new server on this host and port, turn off SSL
    $server = new Server('127.0.0.1', 9502, false);

    // Handle the root path
    $server->handle('/', function(Swoole\Http\Request $request, Swoole\Http\Response $response)
    {
        $response->end("<h1>Index</h1>");
    });

    // Handle another URL path...
    $server->handle('/test', function(Swoole\Http\Request $request, Swoole\Http\Response $response)
    {
        $response->end("<h1>Test</h1>");
    });

    $server->handle('/stop', function(Swoole\Http\Request $request, Swoole\Http\Response $response) use ($server)
    {
        $response->end("<h1>Stop</h1>");
        $server->shutdown();
    });

    $server->start();
});

Custom URL Routing

<?php

use Swoole\Coroutine\Http\Server;

// Create main coroutine context
Co\run(function()
{
    // Create a new server on this host and port, turn off SSL
    $server = new Server('127.0.0.1', 9502, false);

    // Handle all requests via the root path
    $server->handle('/', function(Swoole\Http\Request $request, Swoole\Http\Response $response)
    {
        $requestRoute = $request->server['request_uri'];

        // Example of custom router handling the request path and giving back content
        $content = $router->handle($requestRoute, $request, $response);

        $response->end($content);
    });

    $server->start();
});

Last updated on August 31, 2022