Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Server::__construct(string $host, int $port int $mode int $sock_type)
The IP address of the server:
0.0.0.0
to bind to all addresses on IPv4::
The port of the server (root privilege requried if the port number is less than 1024) to listen on
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 serverOpenSwoole\Server::SIMPLE_MODE
: Reactor based mode, the business logic is running in the reactor thread, simalar to other servers like Nginx
and Node.js
The socket type of the server:
OpenSwoole\Constant::SOCK_TCP
: TCPOpenSwoole\Constant::SOCK_TCP6
: TCP IPv6OpenSwoole\Constant::SOCK_UDP
: UDPOpenSwoole\Constant::SOCK_UDP6
: UDP IPv6OpenSwoole\Constant::UNIX_DGRAM
: Unix socket dgramOpenSwoole\Constant::UNIX_STREAM
: Unix socket stream$sock_type | OpenSwoole\Constant::SSL
. Check configuration for SSLA OpenSwoole\Server
object
Creates a new OpenSwoole Server object.
<?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;
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.
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