Swoole\Server::__construct

Declaration

<?php
Swoole\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:

  • SWOOLE_PROCESS: Multi process mode, the business logic is running in the child processes, this is the default running mode of a server
  • SWOOLE_BASE: 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:

  • SWOOLE_SOCK_TCP: TCP
  • SWOOLE_SOCK_TCP6: TCP IPv6
  • SWOOLE_SOCK_UDP: UDP
  • SWOOLE_SOCK_UDP6: UDP IPv6
  • SWOOLE_UNIX_DGRAM: Unix socket dgram
  • SWOOLE_UNIX_STREAM: Unix socket stream
  • To enable SSL: $sock_type | SWOOLE_SSL. Check configuration for SSL

Return

server

A Swoole\Server object

Description

Creates a new Swoole Server object.

Example

<?php
$server = new Swoole\Server(string $host='0.0.0.0', int $port = 0, int $mode = SWOOLE_PROCESS, int $sockType = SWOOLE_SOCK_TCP);

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

// Because port is set to 0, a random port is used
$port = $server->addListener("0.0.0.0", 0, SWOOLE_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 Swoole Server

Swoole 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.

swoole.socket

[Unit]
Description=Swoole Socket

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

swoole.service

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

[Install]
WantedBy = multi-user.target

server.php

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

$http->set([
    'daemonize' => true,
    'pid_file' => '/var/run/swoole.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 swoole.socket
sudo systemctl start swoole.socket
sudo systemctl start swoole.service