OpenSwoole\Server::__construct

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

Declaration

<?php OpenSwoole\Server::__construct(string $host, int $port int $mode int $sock_type)

Parameters

host

The IP address of the server:

  • You can listen on 0.0.0.0 to bind to all addresses on IPv4
  • There is support for IPv6 and listening on all addresses with ::
port

The port of the server (root privilege requried if the port number is less than 1024) to listen on

mode

Which mode to start the server in:

  • OpenSwoole\Server::POOL_MODE: Multi process mode, the business logic is running in the child processes, this is the default running mode of a server
  • OpenSwoole\Server::SIMPLE_MODE: Reactor based mode, the business logic is running in the reactor thread, simalar to other servers like Nginx and Node.js
sock_type

The socket type of the server:

  • OpenSwoole\Constant::SOCK_TCP: TCP
  • OpenSwoole\Constant::SOCK_TCP6: TCP IPv6
  • OpenSwoole\Constant::SOCK_UDP: UDP
  • OpenSwoole\Constant::SOCK_UDP6: UDP IPv6
  • OpenSwoole\Constant::UNIX_DGRAM: Unix socket dgram
  • OpenSwoole\Constant::UNIX_STREAM: Unix socket stream
  • To enable SSL: $sock_type | OpenSwoole\Constant::SSL. Check configuration for SSL

Return

server

A OpenSwoole\Server object

Description

Creates a new OpenSwoole Server object.

Example

<?php
$server = new OpenSwoole\Server(string $host='0.0.0.0', int $port = 0, int $mode = OpenSwoole\Server::POOL_MODE, int $sockType = OpenSwoole\Constant::SOCK_TCP);

// Mixing TCP and UDP listeners to monitor different ports at the same time
$server->addlistener("127.0.0.1", 9502, OpenSwoole\Constant::SOCK_TCP);              // TCP listener
$server->addlistener("192.168.1.100", 9503, OpenSwoole\Constant::SOCK_TCP);          // Web Socket listener
$server->addlistener("0.0.0.0", 9504, OpenSwoole\Constant::SOCK_UDP);                // UDP listener
$server->addlistener("/var/run/myserv.sock", 0, OpenSwoole\Constant::UNIX_STREAM);   // UnixSocket Stream
$server->addlistener("127.0.0.1", 9502, OpenSwoole\Constant::SOCK_TCP | OpenSwoole\Constant::SSL); //TCP + SSL

// Because port is set to 0, a random port is used
$port = $server->addListener("0.0.0.0", 0, OpenSwoole\Constant::SOCK_TCP);

// See which port was selected
echo $port->port;

Other

Listening a random port

The swoole server supports the feature of listening on a random port. When the argument $port is not or is 0, the server will choose a random and available port to listen on. You can use $server->port to find out which port was selected.

Systemd setup for OpenSwoole Server

OpenSwoole adds support so you can use a server with systemd socket. The port listened on can be set by the configuration of systemd.

Checkout the configuration for how to daemonize a server.

openswoole.socket

[Unit]
Description=OpenSwoole Socket

[Socket]
ListenStream=9501
Accept=false
[Install]
WantedBy = sockets.target

openswoole.service

[Service]
Type=forking
PIDFile=/var/run/openswoole.pid
ExecStart=/usr/bin/php /var/www/openswoole/server.php
ExecStop=/bin/kill $MAINPID
ExecReload=/bin/kill -USR1 $MAINPID

[Install]
WantedBy = multi-user.target

server.php

<?php
$http = new OpenSwoole\HTTP\Server("systemd");

$http->set([
    'daemonize' => true,
    'pid_file' => '/var/run/openswoole.pid',
]);

$http->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

Start the systemd service

sudo systemctl enable openswoole.socket
sudo systemctl start openswoole.socket
sudo systemctl start openswoole.service
Last updated on September 20, 2022