Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\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 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.
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)
});
});