Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Server->after(int $millisecond, mixed $callback)
The timer after interval in milliseconds, the timer will only be used once after the millisecond delay has been reached
The callback that the timer will run once and be destroyed afterwards
If success, it returns a unique ID of the timer, otherwise it returns false
.
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.
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)
});
});