Swoole\Server->on('start', fn)

Declaration

<?php
Swoole\Server->on('start', callback $callback)

Parameters

event

The event name

callback

callback function

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Executue the callback function when the Server is started.

Before the start stage, the Server has finished the stages:

  • Create the Manager Process
  • Create the Worker Processes
  • Listen on the TCP/UDP port
  • Started the Swoole Timer

After the start stage, the reactor threads are ready to process event and accept connections.

In the callback function registered on start, only limited functions should be defined. Such as logging, record and modify the name of process, record the process ID etc .

The stages start and workerstart are started the same time in different processes.

It is recommended to save the value of $server->master_pid and $server->manager_pid into file in the start callback function. Then you are able to send Linux signals to control these processes.

This stage can't be used in SWOOLE_BASE mode.

Example

<?php
$server = new Swoole\Server("127.0.0.1", 9501);
$server->on('start', function ($server){
    echo "Server is started.\n";
});
$server->on('connect', function ($server, $fd){
    echo "New connection established: #{$fd}.\n";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
    $server->send($fd, "Echo to #{$fd}: \n".$data);
    $server->close($fd);
});
$server->on('close', function ($server, $fd) {
    echo "Connection closed: #{$fd}.\n";
});
$server->start();