0

I am using PHP and AJAX requests to get the output of a program that is always running and print it on a webpage at 5 second intervals. Sometimes this log file can get up to 2mb in size. It doesn't seem practical to me for the AJAX request to fetch the whole contents of this file every 5 seconds if the user has already gotten the full contents at least once. The request just needs to get whatever contents the user hasn't gotten in a previous request.

Problem is, I have no clue on where to begin to find what contents the user hasn't received. Any hints, tips, or suggestions?

Edit: The output from the program starts off with a time (HH:MM:SS AM/PM), everything after has no pattern. The log file may span over days, so there might not be just one "02:00:00 PM" in the file, for example. I didn't write the program that is being logged, so there isn't a way for me to modify the format in which it prints it's output.

David S.
  • 3
  • 2
  • Is there line numbers or ids that are being sent? If so, then update the last id every time the user gets information. – Tim Withers Jul 25 '12 at 20:56
  • No there aren't any line numbers, I edited the above post with a little more info on the log's format. And thanks Gabriel! – David S. Jul 25 '12 at 21:06

2 Answers2

2

I think using a head request might get you started along the right path.

check this thread: HTTP HEAD Request in Javascript/Ajax?

if you're using jquery, it's a simple type change in the ajax call:

$.ajax({url: "some url", type: "HEAD".....});

personally, I would check the file size & date modified against the previous response, and fetch the new data if it has been updated. I'm not sure if you can fetch only parts of a file via ajax, but I'm sure this can be accomplished via php pretty easily, possibly this thread may help:

How to read only 5 last line of the text file in PHP?

Community
  • 1
  • 1
nebulae
  • 2,665
  • 1
  • 19
  • 16
  • I forgot about head request! So pretty much just compare the file size returned to the size of whatever the user has already received of the the log file. If not, calculate an offset to request from (offset is then handled by php)? – David S. Jul 25 '12 at 21:21
  • yup :) That's the route I would take. – nebulae Jul 25 '12 at 21:25
  • Thanks! Seems simple enough. That head request will come of great use for this. – David S. Jul 25 '12 at 21:49
0

It depends how your program is made and how does it print your data, but you can use timestamps to reduce the amount of data. If you have some kind of IDs, you should probably use them insteam of timestamps.

mdziekon
  • 3,531
  • 3
  • 22
  • 32