I'm using Server-Sent Events, to print messages for user.
In infinite loop, every 10 seconds I check if there is any new item in cache to broadcast:
$messages_to_broadcast = $this->_cache->getItemsByTag('inbox_message');
foreach ($messages_to_broadcast as $key => $_message) {
$_message = $_message->get();
if($_message->recipient == $this->_user_id || $_message->recipient == 0){
if(!is_null($html = \CRM\Engine\MessagingService::getMessageToBroadcast($_message)))
{
echo "event: $_message->type \n";
echo "data:{\n";
echo "data:\"message_html\": \"$html\" \n";
echo "data:}\n\n";
$this->send_keepalive = false;
$this->_cache->deleteItem($key);
}
}
}
At irregular intervals, there is event, which save message to cache:
$_cache_this = self::$_cache->getItem("message_".$_message->id);
if(!$_cache_this->isHit()){
$_cache_this->set($_message)
->expiresAfter(600)
->addTag('inbox_message');
self::$_cache->save($_cache_this);
}
The problem is that while I check in infinite loop for new items in cache, I get empty array. When I reload page, or browser reconnect to Server Side Events stream, item appears in cache. Is there any flush
method I'm missing here?
I'm using files
as cache method.
\phpFastCache\CacheManager::setDefaultConfig(array(
"path" => DIR_TMP
));
global $cache;
$cache = \phpFastCache\CacheManager::getInstance('files');