Demo.php file
<?php
function write_back()
{
echo file_get_contents("sample.html");
sleep(2);
write_back();
}
write_back();
?>
Resulted me this error Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\omnama\demo.php on line 8
I was echoing back the html contents from the php script to the ajax request for every 2 seconds.
Demo.html file
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","demo.php",true);
xmlhttp.send();
}
Let me explain what I am trying to do. I have sample.html file that keeps on updating, it may contain number of users logged in currently or the number of people available to talk , something like that.
I have used an html file because whenever a new user logs in, the html file will be updated with the user name using PHP file concepts and then these html file should be updated to all the online users.So im just updating <div id="myDiv"> tag with the response given by demo.php file
I have used HTML file because requesting MySql everytime to check if anyone logged in, will cause a serious server problem thats why i wanted to use html file.but echoing back the file contents every time is giving me these error.
I was thinking to do this. just check the last modified time of the file and if it is different from the previous one then echo its contents , is this the right way ?
Could anyone explain me how do i keep track of sample.html and update it contents to the logged in users? I need some help.Thanks
There are probably a few ways to go about it and eventually I guess it comes down on how many requests/updates you are having.
However I think to begin with you need to decide how you are going to retrieve the results you are interested in.
Easy solution: You could use setInterval() and have the browser perform a new XmlHttpRequest on the server (POLL) to retrieve the new list of users at certain predefined intervals
More complicated solution: You could establish a “web-socket” like connection between the server and the browser and PUSH down to the browser the user’s list. You could set up a comet like server, long-polling custom implementation, or a socket.io (http://http://socket.io/ – I am a huge fan) and have the server notify the listening browsers when something worth notifying occurs.
Now to the server side:
Personally I don’t really like your approach having to write to an html file to avoid database queries. The obvious way is to query the database for new users upon request and reply back. Are there so many users that would cause a bottleneck for the server? If so could you add more servers to it? (mysql replicated just to serve this?) You could probably speed up the queries if you added a timestamp on the requests made (i.e. tell me what has changed since XXX)
Have you though of other alternatives? I.e. if you end up using socket.io your whole online-offline status could be handled by the socket status on the server side without anything else. (socket.io server knows which clients are connected to it).
Another idea could be to keep the online users to a memory storage such as memcached or a redis server. So on each login/logout you update a memcached server and accordingly retrieve the results from it.
I could probably think even more ways to go about it, if I spend more time on it. However what you need to make sure is if you actually need all these and you can’t work with a simple SQL query.