Jump to content

polling a server and to check for a file state change.


fxr

Recommended Posts

I am not sure where to put this question, hopefully here is ok.

 

I have a web app that needs its clients to poll a server every few seconds to check if a specific file has changed. If the file has changed it sends an alert to the client [a swf gets played] .

 

My scripts work fine the majority of time and for the majority of users but my method seems to be issuing a phantom alert to some but not all clients at around the time the server switches over to a new day(midnight).

 

It uses a cookie,  which is created on page load to store the last modification date of a file on the server. That cookie value is then later compared with the files' modification date that is returned by a php script called via an ajax request [which is issued every few seconds]. What the file contains is irrelevant as i dont want to bog the server down by opening and reading files every few seconds by maybe 200 clients.

 

i will include relevant code snippets , [it uses the prototypejs library for issuing ajax requests]

 

// on page creation  
// get file mod time and set cookie lastChange
<?php 
session_start();
$value = filemtime('../check/test.txt');
setcookie("lastChange", $value);
?>

function checkChanged() {
new Ajax.Request("check_changed.php", {
method: 'get',
onSuccess: function(transport){  var x = get_cookie ( "lastChange" );
var response = transport.responseText;  
if (x != response && response != 0)  // cookie != last file modification time or invalid response.
	{swfobject.getObjectById("siren").Play();
	set_cookie ( "lastChange", response );}}});}


//checkchanged.php

<?php
if($timeS = filemtime('../check/test.txt'))
 { echo $timeS;}
else echo 0; 
?>

 

 

Now i believe my issue may be caused by this:

 

http://uk.php.net/clearstatcache

 

is anyone able to confirm that this is indeed my issue? and if it is what sort of performance hit would i suffer if i put

clearstatcache();

at the beginning of my checkchanged.php script?

 

i would also like to know if there is any other solutions i could use.. and whether or not the way i have decided to approach this problem is entirely foolhardy?

 

I welcome any comments at all, as this issue has bugged me persistently since i have embarked on this project and its pretty much critical to its success.

 

 

thanks.

 

Link to comment
Share on other sites

ok i decided to inspect the http response headers of my test.txt.. meaning i dont need no cookies or server side scripts.. surely the most efficient way of checking for that state change. ll include the code.

 


var LastMod;
function checkChanged() {
    new Ajax.Request("http://XXX.com/check/test2.txt", {
    onSuccess: function(transport){
        var header = transport.getResponseHeader('Last-Modified');
        if (LastMod != header && LastMod !=null)
        {LastMod = header;swfobject.getObjectById("siren").Play();}
        else LastMod = header;
        }});}

 

very happy with my solution :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.