-1

After 5 hours of trying I'm coming to you.

I need to create a page, that will automatically update himself and display file content when file changed. Let say we have foo.txt, that's updating continuously. I don't want page to reload every n seconds. Additionally I need only last 30 lines of this file displayed. I came up with this:

<?php   //live.php
$handle = popen("tail -30l foo.txt 2>&1", 'r');
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo "$buffer<br/>\n";
    ob_flush();
    flush();
}
pclose($handle);
?>

what successfully displays last 30 lines of file, but not updating. That's where I'm stuck - I don't know how to achieve below tasks:

  1. Check for file update,
  2. Display live.php when file changes.

Can I use Ajax update feature with this?

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
Bielecki
  • 131
  • 1
  • 2
  • 13
  • First of all, When do you want to show the updated last three lines ?. Instantly or after some period of time ? – squiroid Jan 11 '17 at 03:29
  • ASAP, so instantly. – Bielecki Jan 11 '17 at 03:35
  • Now you can only do this if you check for the change from the server via ajax call. And only upload the page if there is any change. So I would suggest do one thing create the hash of the current 30 lines and send it to server and compare that hash with the current 30 lines on server side. If there is change send the new 30 lines back if not then just send flag with false value. And if you want other way you need to create a socket connection from server to frontend. If there is any change in the file (use CRON JOB) to check then send it via socket to front end and display. – squiroid Jan 11 '17 at 03:39
  • Isn't it simpler to check if the file timestamp or size has changed? And de facto I can imagine what code should do, I just don't know how it should be written :( – Bielecki Jan 11 '17 at 03:43
  • Yes, anyway you like to check the change on the server side. – squiroid Jan 11 '17 at 03:46
  • Yeah, of course. It wouldn't make any sense to check it on client side, because I'd like to minimize needed transfer. – Bielecki Jan 11 '17 at 03:47

1 Answers1

0

There is two ways of doing it.

1) Pooling This will be done by frontend mainly. You need to check after every second from front end it there is any change it the data. If change is detected send the new data from the server and replace the current data with new data. If no change is found there is no need to replace.

PROS:

i) Page will only refresh if there is any change on server.
ii) Client is responsible for detection manily.

2) Pushing This can be achieved by server mainly. Whenever there is any change in the file detect at your PHP end (Via CRON JOB). Send the data to front end via socket.

PROS:

 i) Only data will send if there is change in data. No extra network calls.

This article will help.

In what situations would AJAX long/short polling be preferred over HTML5 WebSockets?

Community
  • 1
  • 1
squiroid
  • 13,809
  • 6
  • 47
  • 67