OpenSwoole Server after

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\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 OpenSwoole\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)
    });
});
Last updated on September 1, 2022