3

I have a file that gets new lines appended to it frequently. I want to create a web application running in the browser using javascript that will be able to detect changes on that local file and print the new content that is added on the file.

My solution includes using javascript polling with setInterval and the new HTML5 File API. I load the file using an input file field and then poll the size of the file. Each time the size changes (in my case increases) this indicates to me that new content has been added to the file. I then slice the file using a Blob and only take the content that was appended using the old file size as start and the new file size as end.

Generally it works good with an average interval (I'm using 500ms) but I'm curious if there's a better solution cause this one sounds a bit like a hack to me.

John Papastergiou
  • 999
  • 2
  • 14
  • 29

1 Answers1

0

You can use js-logtail for following files like tail -f :

<head>
    <title>habitat parser log viewer</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="logtail.js"></script>
    <link href="logtail.css" rel="stylesheet" type="text/css">
</head>

<body>
    <div id="header">
        js-logtail.
        <a href="./">Reversed</a> or
        <a href="./?noreverse">chronological</a> view.
        <a id="pause" href='#'>Pause</a>.
    </div>
    <pre id="data">Loading...</pre>
</body>

You don't even need a server side part, the webserver (as apache, lighthttpd) handles this with the HTTP Range header internally.

WeSee
  • 3,158
  • 2
  • 30
  • 58
  • 1
    This is for remote files, not local files. – Alexander Craggs Dec 03 '17 at 02:02
  • Ok, this is a more general solution which works for local files, too, if there is a webserver available. – WeSee Dec 03 '17 at 13:14
  • 1
    That's a fair point, however, the chances of every client visiting your website having a webserver setup to provide specific files is unlikely. Just checking, you realise LogTail appears to be setup to tail files from the server? Not from the client? Hence why it uses XHR requests and not HTML5 File API as suggested in the question. – Alexander Craggs Dec 04 '17 at 04:37