sergonie/network

PHP swoole based http server

dev-develop 2021-05-27 20:04 UTC

This package is auto-updated.

Last update: 2022-07-27 23:31:04 UTC


README

License: MIT Build Status Scrutinizer Code Quality Code Coverage

Requirements

  • PHP 7.4 or better
  • Swoole extension is required for network server to work

Installation

Linux users:

pecl install swoole
composer install sergonie/network

Mac users with homebrew:

brew install swoole
composer install sergionie/network

or:

brew install homebrew/php/php71-swoole
composer install sergonie/network

Basic Usage

<?php
// Autoloader.
require_once __DIR__ . '/vendor/autoload.php';

// Create server instance.
$server = new \Sergonie\Network\Server();
$server->start();

Listeners

Sergonie http server uses event-driven model that makes it easy to scale and extend.

There are five type of events available, each of them extends Sergonie\Network\Server\Listener interface:

  • Sergonie\Network\Server\OnStartListener fired when server starts
  • Sergonie\Network\Server\OnStopListener fired when server stops
  • Sergonie\Network\Server\OnConnectListener fired when new client connects to the server
  • Sergonie\Network\Server\OnCloseListener fired when connection with the client is closed
  • Sergonie\Network\Server\OnRequestListener fired when new request is dispatched
<?php
// Autoloader.
require_once __DIR__ . '/vendor/autoload.php';

use Sergonie\Network\Server\Client;
use Sergonie\Network\Server\OnRequestListener;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Sergonie\Network\Http\Stream;

// Create server instance.
$server = new \Sergonie\Network\Server();

// Each request will retrieve 'Hello' response
$server->addListener(new class implements OnRequestListener {
    public function onRequest(Client $client, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
       return $response->withBody(Stream::fromString("Hello world"));
    }
});
$server->start();

Configuration

Server can be easily configured with Sergonie\Network\Server\Configuration class.

Please consider following example:

<?php
// Autoloader.
require_once __DIR__ . '/vendor/autoload.php';

// Listen on localhost at port 80.
$configuration = new \Sergonie\Network\Server\Configuration('0.0.0.0', 80);

// Create server instance.
$server = new \Sergonie\Network\Server($configuration);
$server->start();
Enabling ssl support
<?php
// Autoloader.
require_once __DIR__ . '/vendor/autoload.php';

$configuration = new \Sergonie\Network\Server\Configuration();
$configuration->enableSsl($certFile, $keyFile);

// Create server instance.
$server = new \Sergonie\Network\Server($configuration);
$server->start();
Running server as a daemon
<?php
// Autoloader.
require_once __DIR__ . '/vendor/autoload.php';

$configuration = new \Sergonie\Network\Server\Configuration();
$configuration->enableDaemon($pidFile);

// Create server instance.
$server = new \Sergonie\Network\Server($configuration);
$server->start();

More examples can be found in the ./examples/ directory.