I'm creating a chat bot, I have php workers picking up the outgoing messages from a message queue. I am getting an array of messages from the payload (from the queue). I want to send out the messages with a lag in order to imitate the 'typing'.
while (true) {
$messages = pickMsgFromQueue();
sendMessagesWithDelay($messages);
}
function sendMessagesWithDelay(array $messages) {
if (empty($messages)) {
return;
}
sendMessage(shift($messages));
sleep(1); // This will take 99.9 % of script time obviously
sendMessagesWithDelay($messages);
}
function sendMessage($message) {
// some curl call;
}
The problem is, the bot worker will be sitting on the sleep()
function for most of the time and the only solution I have right now is to have a lot of workers running at the same time so a new message on the queue gets picked up as soon as possible.
Is there a good way to solve this problem without multithreading or starting another php process?
cheers!