Swoole Server on

public void Swoole\Server::on ( string $event_name , callable $callback )

Alias: Swoole\Server->on ( string $event_name , callable $callback )

Register callback function for the event


  • $event the string of event to be registered
  • $callback the callable type variable

Check the full list of events

Check the four types of callbacks


The result if it has been registered successfully


$server = new Swoole\Server("", 9501);
$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->on('close', function ($server, $fd) {
    echo "Connection closed: #{$fd}.\n";