I have the following call ajax;
function notificacion_json(){
/* Cargar notificaciones_respuesta_solicitudes */
var resultado = document.getElementById("span_numero_notificaciones");
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
resultado.innerHTML = xmlhttp.responseText;
//alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST", "php/ajax/notificaciones.php", true);
xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xmlhttp.send('notificacion_json=' + '1');
}
This function I have it within a setInterval
that is repeated every 2 seconds, to call the file notificaciones.php
;
if(isset($_POST['notificacion_json'])){
$usuario_solicitante = $_COOKIE['nombre_usuario'];
$carpeta_destino = $_SERVER[ 'DOCUMENT_ROOT' ] . "/intranet/uploads/perfiles/".$usuario_solicitante;
$nombre_archivo = "/metadata.json";
if(file_exists($carpeta_destino)){
if (file_exists($carpeta_destino.$nombre_archivo)){
$jsondata = fopen($carpeta_destino.$nombre_archivo,"r");
//Si el archivo ya existe
$jsondata = file_get_contents($carpeta_destino.$nombre_archivo); //Leer archivo
$data = json_decode($jsondata, true); //Decodificandolo para convertirlo en un array asociativo
echo $data[$usuario_solicitante]['notificaciones'];
}
}
everything works perfect until a certain number of users connect (currently I have more than 150 simultaneous users) as soon as I implement this feature the web becomes extremely slow.
Is there an alternative to this or maybe I'm wrong with my code?
NOTE: The response size is 239b.
Possible Solution; Instead of being updated every 2 seconds I have placed it every 70 seconds, it would not be in real time but it works and does not overload the network. I will study Node.js to make a system up to the standard.