Coroutine TCP Server

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Experimental feature, not recommended for production use

Swoole Coroutine Server which uses the TCP protocol and allows you to create a server within a coroutine context programmatically on the fly. It is a TCP server which uses UnixSockets for communication.

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

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

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

First available since version v4.4

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

Methods


Server Connection Object

The coroutine server comes with a connection object which is passed to the closure when using the handle() function. You use this object to handle data from the connection.

The OpenSwoole\Coroutine\Server\Connection object provides you with the following functions:

  • recv()

Call this method to receive data from the request, if you have setup protocol parsing then a whole data packet will be sent, otherwise you will receive data in chunks.

<?php
function recv(float $timeout = 0): mixed

The $timeout may be used so that the receive is not waiting forever. Because a float type is used, it means that 1.5 is 1.5 seconds etc.

  • send()

Send back data to the connection client, the data type must be a string type.

<?php
function send(string $data)
  • close()

Call this method to close the client connection to the server.

<?php
function close(): bool
  • exportSocket()

This method is for when you want to get access to the underlying UnixSocket which is being used for the currently connect client. By calling this method you will be returned a socket object which you then have access to more low-level functions.

<?php
function exportSocket(): OpenSwoole\Coroutine\Socket

Refer to the OpenSwoole\Coroutine\Socket object documentation to learn how to interact with the UnixSocket and its extra methods.


Quick Start Example

<?php

use OpenSwoole\Coroutine\Server;
use OpenSwoole\Coroutine\Server\Connection;

co::run(function()
{
    go(function()
    {
        $server = new Server('0.0.0.0', 9501, false);

        $server->handle(function(Connection $conn)
        {
            while(true)
            {
                $data = $conn->recv();
                if(!$data)
                {
                    break;
                }

                $conn->send("hello $data");
            }

            $conn->close();
        });

        echo "Start TCP server :9501\n";
        $server->start();
    });

    go(function()
    {
        $server = new Server('0.0.0.0', 9502, false);

        $server->handle(function(Connection $conn)
        {
            while(true)
            {
                $data = $conn->recv();
                if(!$data)
                {
                    break;
                }

                $conn->send("hello $data");
            }

            $conn->close();
        });

        echo "Start TCP server :9502\n";
        $server->start();
    });
});

Multi Process Server Example

To understand how to utilise this module fully, checkout the full server and multi process example.

This example shows you how to manage multiple servers via a multi-process model using the Swoole Process Pool.

Last updated on September 1, 2022