Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\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:
SWOOLE_PROCESS
: Multi process mode, the business logic is running in the child processes, this is the default running mode of a serverSWOOLE_BASE
: 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:
SWOOLE_SOCK_TCP
: TCPSWOOLE_SOCK_TCP6
: TCP IPv6SWOOLE_SOCK_UDP
: UDPSWOOLE_SOCK_UDP6
: UDP IPv6SWOOLE_UNIX_DGRAM
: Unix socket dgramSWOOLE_UNIX_STREAM
: Unix socket stream$sock_type | SWOOLE_SSL
. Check configuration for SSLA Swoole\Server
object
Creates a new Swoole Server object.
<?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;
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.
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