Swoole Server after

Declaration

<?php
Swoole\Server->after(int $millisecond, mixed $callback)

Parameters

milliseconds

The timer after interval in milliseconds, the timer will only be used once after the millisecond delay has been reached

callback

The callback that the timer will run once and be destroyed afterwards

Return

timerId

If success, it returns a unique ID of the timer, otherwise it returns false.

Description

Trigger a timer to run after the specified time in milliseconds, the timer will only run once and will be destroyed once it finishes. This method is just an alias of function Swoole\Timer::after, please refer to the timer documentation for more information about Swoole Timers.

If the server reloads ro restarts, timers are stopped. If timers are handling critical data, you should implement some functionality to gracefully bring down the timers upon the onWorkerStop event.

Example

Using timer after inside the receive callback:

<?php
Swoole\Server->after(int $after_time_ms, mixed $callback)

$server->on('receive', function ($server, $fd, $from_id, $data)
{
    // A timer that runs once after the delay has been reached
    Swoole\Server->after(3000, function() use($fd)
    {
        var_dump("fd: " . $fd)
    });
});