Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    fxr
    fxr is offline
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    polling a server to check for a file state change.

    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]

    Code:
    // 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
    Code:
    clearstatcache();
    at the beginning of my checkchanged.php script?

    i would also like to know if there is any other [more efficient] 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.

  • #2
    fxr
    fxr is offline
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    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

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,298
    Thanks
    10
    Thanked 584 Times in 565 Posts
    doing a head request instead of GET virtually guarantees a single packet ping, but if the text file is small enough, it shouldn't make a huge difference.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •