Weedoze Weedoze - 1 year ago 90
Node.js Question

Javascript - Reading from a frequently updated file

I am currently writing a website for an embeded system. The objective is to read a log file and modifiy a json file with the log file content.

The log file is being constantly written to by another program

The web application will be done with NodeJs and socket.io. Maybe is there another way to display those log data in real time on the html page ?

If I just open the log file, will it lock it ? Will I have the time to open it, do the algorithm and close it in 1 second ?

Is there a way to keep it open and always read the last line ?

I found this post : "Reading from a frequently updated file" but it is with Python.

Additonal info
The program which writes in the log file can be updated. One of the possibility is to edit a file with a single line (which is the last one from the big log file). This unique line will be updated every second

Answer Source

If you're going to use Node.js I recommend you use node-tail.

It allows you to "tail" the file in a straight forward manner. According to the documentation this is how you'd do it:

Tail = require('tail').Tail;

tail = new Tail("fileToTail");

tail.on("line", function(data) {

tail.on("error", function(error) {
    console.log('ERROR: ', error);

Also, you could use fs.watchFile which comes with Node.js.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download