Documentation Packages Support

Http Server ⋅ HttpServer

The HttpServer instance controls the whole listening and dispatches the parsed requests.

The HttpServer class has a custom debug output, which isn’t part of the public API and which shouldn’t be relied upon.

This class was previously named Server. An alias to Server name exists for backward-compatibility with v1.x, but may be removed in a future version.


public function __construct(
    Amp\Socket\Server[] $servers,
    RequestHandler $requestHandler,
    Psr\Log\LoggerInterface $logger,
    Options $options = null


Amp\Socket\Server[] $servers List of socket servers.
RequestHandler $requestHandler Request handler interface.
Psr\Log\LoggerInterface $logger A PSR compliant logger (eg. amphp/log).
$options HTTP server settings.
null creates an Options object with all default options.

$servers must be a non-empty list of Amp\Socket\Server objects. Otherwise an \Error will be thrown.

setDriverFactory(HttpDriverFactory $driverFactory): void

Define a custom HTTP driver factory.

setClientFactory(ClientFactory $clientFactory): void

Define a custom client factory.

setErrorHandler(ErrorHandler $errorHandler): void

Sets the error handler instance to be used for generating error responses.

getState(): int

Returns the current server state, which is one of the following class constants:

  • Server::STARTING
  • Server::STARTED
  • Server::STOPPING
  • Server::STOPPED

getOptions(): Options

Returns the server options object.

getErrorHandler(): ErrorHandler

Returns the error handler.

getLogger(): Psr\Log\LoggerInterface

Returns the logger.

getTimeReference(): TimeReference

Returns the time context.

attach(ServerObserver $observer)

Enables a ServerObserver instance to be notified of the updates.

start(): Promise

Starts the server.

stop(int $timeout = 3000ms): Promise

Stops the server. $timeout is the number of milliseconds to allow clients to gracefully shutdown before forcefully closing.

The returned Promise will resolve when the server has successfully been stopped.