4

I'm looking for a way through AJAX (not via a JS framework!) to real time monitor a file for changes. If changes where made to that file, I need it to give an alert message. I'm a total AJAX noob, so please be gentle. ;-)

Edit: let me explain the purpose a bit more in detail. I'm using a chat script I've written in PHP for a webhop, and what I want is from an admin module monitor the chat requests. The chats are stored in text files, and if someone starts a chat session a new file is created. If that's the case, in the admin module I want to see that in real time.

Makes sense?

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
user890201
  • 43
  • 1
  • 4
  • what is your setup? is the file on a server and you want to monitor it for changes from a client elsewhere on the network, or something else? – jches Aug 11 '11 at 15:24
  • The setup in this case is localhost, the script must monitor a file that is accessible through HTTP on the same server the script is. It's a textfile by the way. I'm using Apache 2, PHP on a Mac OS X 10.7 server. – user890201 Aug 11 '11 at 15:28

3 Answers3

6

To monitor a file for changes with AJAX you could do something like this.

var previous = "";

setInterval(function() {
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            if (ajax.responseText != previous) {
                alert("file changed!");
                previous = ajax.responseText;
            }
        }
    };
    ajax.open("POST", "foo.txt", true); //Use POST to avoid caching
    ajax.send();
}, 1000);

I just tested it, and it works pretty well, but I still maintain that AJAX is not the way to go here. Comparing file contents will be slow for big files. Also, you mentionned no framework, but you should use one for AJAX, just to handle the cross-browser inconsistencies.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • I can't install FAM, but thanks for the tip, it might be of use to someone else! – user890201 Aug 11 '11 at 15:30
  • What about using `sha1_file` (http://php.net/manual/en/function.sha1-file.php) to calculate the hash of a file, store it somewhere, and compare the previous one with the current hash to see if there have been any changes. – Alex Turpin Aug 11 '11 at 15:39
  • That still does not solve the real time monitoring issue (see my edit in the original post for more details). It needs to be done not by running PHP scripts in the background, but via client side scripting. I know it can be done somehow through AJAX. I've seen it before. – user890201 Aug 11 '11 at 15:49
  • You could always check for changes in a certain file through AJAX, but not for new files created, unless you already know what it's gonna be called. You could always have another file called something static like "conversations.json" and have it list all the current conversations. – Alex Turpin Aug 11 '11 at 15:57
  • What I can do is push the new chat requests into single file where I store the path to the newly created file. Those files I can read in PHP and send a notification to the admin dashboard. Now how (with sample code would be highly appreciated!) do I monitor that single file using AJAX with a automatic refresh? – user890201 Aug 11 '11 at 16:01
  • Edited my post with an example – Alex Turpin Aug 11 '11 at 16:19
  • Now that is absolutely brilliant! Just what I needed. Only thing is that it gives a JS alert twice when the file just changed once. – user890201 Aug 11 '11 at 16:25
  • I don't know about that, might have to do with the browser you're using and it's implementation of AJAX. As I said, use a framework for XHR. If you've found my answer helpful, please consider accepting it as the answer for your question. – Alex Turpin Aug 11 '11 at 16:26
  • Thanks, you've been a great help. I'll manage from here. Safari does it twice, FireFox just once, so it must be a browser issue. – user890201 Aug 11 '11 at 16:31
0

AJAX is just a javascript, so from its definition you do not have any tool to get access to file unless other service calls an js/AJAX to notify about the change.

m4risU
  • 1,231
  • 11
  • 14
0

I've done that from scratch recently.

I don't know how much of a noob you are with PHP (it's the only server script language I know), but I'll try to be as brief as possible, feel free to ask any doubt.

I'm using long polling, which consists in this (

  1. Create a PHP script that checks the content of the file periodically and only responds when it sees any change (it could include a description of the change in the response)
  2. Create your XHR object
  3. Include your notification code as a callback function (it can use the description)
  4. Make the request
  5. The PHP script will start checking the file, but won't reply until there is a change
  6. When it responds, the callback will be called and your notification code will launch

If you don't care about the content of the file, only that it has been changed, you can check the last-modified time instead of the content in the PHP script.

EDIT: from some comment I see there's something to monitor file changes called FAM, that seems to be the way to go for the PHP script

rafa
  • 326
  • 3
  • 9