2

I have been searching everywhere for a method of streaming a text file. By streaming I mean I want the page to remember its location, and everytime a new entry is added, it will add it to the bottom.

Example (Displayed on page from txt file)

Jack

John

Ellis

(text file is updated to)

Jack

John

Ellis

Emma

Carly

I want the page to pick up the changes without having to refresh the whole page.

Im learning alot. I have figured out how to display the text files on the page via php (my knowledge base is stuck with php/html only self taught). I can get it to refresh, but im having to use javascript to auto scroll down the page to the bottom everytime, and have the page refreshing every few seconds.

Any ideas or tips?

user2905326
  • 50
  • 1
  • 7

2 Answers2

2

Well, I would not do this the Shrek's Donkey way:

"Are we there, yet?", 5 seconds later, "Are we there, yet?"

Are we there, yet?", 5 seconds later, "Are we there, yet?

What is the problem with this?

Bandwidth consumption, pure and simply.

What should I do?

The answer is: Server Push. Or something like Server Push, since is not possible to start things from the server with HTTP.

Instead of poll the server every 5 seconds if there is a new version of the file, why not let the server notify you just when it really has changed?

And how do I do this?

The answer is: with Ajax, but with a different approach.

The high level algorithm is:

  1. Send an Ajax request to the server from the browser.
  2. When the server receives the request, verify if the file has changed.
    1. If it has changed, then return the new data.
    2. If not, make the server sleep for a while and then go back to step 2.
  3. If a long time has passed and there is no modification, the server could (or must) return a response, with no data bounded to it.

How can I check if the file has changed?

Instead of reading the file and comparing the data, a better approach is to send with the request the timestamp of when the file had it last change. You can do this with the filemtime funciton.

On the server, you verify if the file last modified time is bigger than the one coming from the request. If it is, then you read the file and send the response along with the new file modified time (step 2a). If not, go for step 2b and use the usleep function to make the server sleep for a while and save CPU.

To know if a long time has passed and there's no change, you can use the microtime function at the beginning of the script and make the difference of its value on every iteration. If there has been past much time, you'll send an empty response.

Making a draft, the server-side script would be like:

$startTime = microtime();
$filePath = '/path/to/file.txt';
$lastModifiedTime = $_GET['lastModifiedTyme']; // Supposing it comes from the query string

$currentModifiedTime = null;
while ($currentModifiedTime = filemtime($filePath) < $lastModifiedTime) {
    usleep(1000); // dorme por 1 seg.

    // If has passed more than 1 minute
    if ((microtime() - $startTime) > 60000) {
        header('HTTP/1.1 304 Not Modified'); // Or simply http_response_code(304) for PHP 5.4+
        exit;
    }
}

$data = file_get_contents($file);
$jsonResponse = json_encode(array(
    'data' => $data,
    'modifiedTime' => $currentModifiedTime
);

echo $jsonResponse;

On the client side, you'll have to re-run the request every time you receive a response. This could (and should) have a litte delay.

It sounds like a kind of an overhead, I know, but just for you to know that there are another ways of doing this.

Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
0

What I'd recommend you do is use AJAX to periodically call a PHP file which loads the text file and update your web content to display the new content.

It will be similar to this: Refresh a table with jQuery/Ajax every 5 seconds

Community
  • 1
  • 1
Sirago
  • 101
  • 3