Open Swoole Coroutine TCP/UDP Client

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


Latest version: pecl install openswoole-22.1.2

Coroutine TCP/UDP PHP client.

This client uses coroutines and UnixSockets so that it can operate in a non-blocking manner, Swoole handles the scheduling of coroutines, so this client is very powerful.

You can use this client to connect to servers, send, receive and act as a TCP/UDP client over a network.

Instead of using the Coroutine Socket API, You can use the Swoole Coroutine TCP/UDP Client API for an easier experience when working with the TCP/UDP protocols.


Methods

Other methods


Example

<?php

use Swoole\Coroutine\Client;

Co\run(function()
{
    $client = new Client(SWOOLE_SOCK_TCP);

    if(!$client->connect('127.0.0.1', 9501, 0.5))
    {
        echo "Connection failed with error: {$client->errCode}\n";
    }

    $client->send("Hello World! - from Swoole\n");

    echo $client->recv();

    $client->close();
});

See the full working example for a more complete setup and some basic error checking.


Client Configuration

This TCP/UDP client has general options which are shared between the others (HTTP and HTTP2), please refer to the general client configuration first. Then you should read the specific configuration options. But they all use the same method to set these options.


Client Timeouts

All network requests (establish a connection, send data and receive data) may time out. This client has a few ways for how you can setup timeouts.

As each Swoole client is written as a built-in class using coroutines, their timeouts are set the same way, refer to the timeout guide to understand how to setup timeouts.


Error Codes

When using this client, you are bound to come across processing errors. This mostly happens when using connect, send, recv and close methods. Most of these methods return false when there is a problem but you can use $client->errCode to check for what went wrong.

Once you have the error code, you can convert it into a message:

<?php
echo socket_strerror($client->errCode);

// Or

echo $client->errMsg;

This will give you a Linux system error message.


Getting the File Descriptor

Once you have created a client it is possible to get its fd:

<?php
echo $client->fd;

$socket = fopen("php://fd/" . $client->fd);

The fd is only available after you have connected to a server.


Class Constants

The client has a number of class constants which can be used for a number of things.

SWOOLE_KEEP

When creating a new client, you can create a persistent connection using SWOOLE_KEEP. This persistent connection can be created like so:

<?php
$client = new Swoole\Client(SWOOLE_SOCK_TCP | SWOOLE_KEEP);
$client->connect('127.0.0.1', 9501);

...

This is how you can create a long lived connection. Using this long lived connection means that any calls to close() does not close the socket, it just means you can quickly reconnect again using the same IP and port as it gets saved for you or a new connection is made if it has been lost.

The advantages of using this long lived TCP connection is:

  • Reduces the number of handshakes and less IO consumption when reconnecting
  • Reduces server side closing and connecting events, quicker response times

Socket Flag Constants

This Coroutine client is based off some of the native PHP socket implementation, so it supports some of the normal socket flags, refer to PHP Sockets: socket_recv(...) for more information.

MSG_WAITALL

Use MSG_WAITALL to indicate that you want to wait until all the data has been received, meaning that the length must be accurate, otherwise a recv call will timeout for waiting.

MSG_DONTWAIT

Non-blocking, don't wait for all data to be completely read, return immediately.

MSG_OOB

Set when you want to process out-of-band-data, lookup TCP out-of-band data for more information.

MSG_PEEK

Receive data from the cache, this allows you to receive data from the buffer but this does not effect calling recv, it will still read all the data, even if you have peeked some before, the file pointer is not changed.

Last updated on August 31, 2022