I'm looking for guidance on a question I can barely grasp as is. I have the content of a .txt file loaded through readfile('stories/'.$sessionid.'.txt');
in PHP. Any user can add text to the .txt file through POST, and I would like for other users with that page open to become notified of (and updated to) the recent file change – whilst the user who did the update doesn't receive an notification about his own update.
I've been having a hard time finding relevant information, since I'm not sure about what I should search for – or even method of approach for that matter (jQuery, AJAX, PHP?). I read something about filemtime
but I don't know how to apply that in this case. I mainly work with front-end, thus I'm not that experienced with this kind of challenges. I kind of found a solution, but it alerts any file-changes all the time, and could possibly put strain on the server (in worst case senario) I think?
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", "stories/<?php echo($sessionid) ?>.txt", true); //Use POST to avoid caching
ajax.send();
}, 1000);
Is there a way to work and develop this code or where should I begin to look and work with? I really don't have a clue since I've never done anything like this before. Any pointers towards relevant keywords, methods, or tutorials I can search for and use would be greatly appreciated!