OpenSwoole Server Working Example

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

Working Server Example

To get started quickly, here is a working code example for a OpenSwoole TCP/UDP Server:

<?php

// Create a new server, listening on IP and port
$server = new OpenSwoole\Server('127.0.0.1', 9501);

// Setup the connection event callback function
$server->on('Connect', function ($server, $fd)
{
    echo "Client: Connect\n";
});

// Setup the receive event callback function
$server->on('Receive', function ($server, $fd, $reactor_id, $data)
{
    $server->send($fd, "Server: {$data}");
});

// Setup and monitor close events
$server->on('Close', function ($server, $fd)
{
    echo "Client: Close\n";
});

// Start the OpenSwoole Server and accept requests
$server->start();

Explanation

The code example above creates a OpenSwoole TCP Server which runs on 127.0.0.1 and listenings on port 9501. The setup and logic is very simple and easy to understand, everything is done within PHP, no need for any external services. Incoming requests will trigger the event callback functions so your business logic can response to different events that happen. For example, when a client sends a network request, the connect event will be called and then follow up with the Receive event, allowing you to process the incoming request, finally the Close event will then be called. So you can see it is quite easy to manage different stages of a request and response to server events within PHP.

Our example shows how you can respond to different events, that is because the server uses an asynchronous programming model. The server runs using an event loop allows us to run an asynchronous server from PHP, the server will listen for events and respond to them. OpenSwoole uses the event loop to trigger certain events, this is where your event callbacks come in and where all your entry points should start for your application.

Once you have setup the PHP script, you can simple execute and run the server by running the following command:

php server.php

Once you have a successfully started server, you can use netstat to monitor the server listening on port 9501.

To check the server listening on the port, you can use netstat -an | grep 9501.

Then you can test the server with telnet, for example:

telnet 127.0.0.1 9501
hello
Server: hello

If the connection fails, make sure the server is listening on the expected port, check any firewall settings and make sure you are using the right IP address.

Extra Server Details

  • The Swoole Server can support thousands of clients at the same time, where each $fd is the unique identifier of the connected client
  • You call $server->send() to respond to a request and send data back from the server, you must include the $fd (File Descriptor) of the client you are sending data to
  • You can force close a client connection with $server->close()
  • If you client suddenly disconnects, the Close event callback function will be called

UDP Server Example

Swoole also has support for running UDP servers, just like our TCP Server example, a UDP server is similar but a major difference is it has no concept of client connections, the UDP protocol will only expect data to be sent, there is no additional checks or confirmation in place like there is with TCP servers.

UDP Server

<?php

// Start a new UDP server on 12.0.0.1, listening on port 9502
$server = new OpenSwoole\Server('127.0.0.1', 9502, OpenSwoole\Server::POOL_MODE, OpenSwoole\Constant::SOCK_UDP);

// Setup the incoming data event callback, called 'Packet'
$server->on('Packet', function ($server, $data, $clientInfo)
{
    var_dump($clientInfo);
    $server->sendto($clientInfo['address'], $clientInfo['port'], "Server:{$data}");
});

// Start the server and begin accepting incoming requests
$server->start();

To start the UDP Server:

php udp_server.php

Test the running server with:

netcat -u 127.0.0.1 9502
hello
Server: hello

The UDP server responds to the Packet event whenever there is a incoming request, you can then process that data and return a response. You can gather client information from $clientInfo but there is no concept of client connections. You can then respond with $server->sendto.

One major difference is a UDP server does not manage client connections, once the data is sent, that is it, there is no concept of a client, the server receives data and processes it, whereas a TCP server is used for more critical data handling tasks. a TCP Server is what is most commonly used on the internet, it is built for reliability, data is tracked so that nothing is lost or gets corrupted in transit. UDP is faster because there is no error checking, lower latency, data packets are just sent and is mostly used for non-critical data transfer.

Last updated on September 20, 2022