Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:32:53+00:00 2026-06-06T13:32:53+00:00

I need to work out the best way to read data that is being

  • 0

I need to work out the best way to read data that is being written to a file, using node.js, in real time. Trouble is, Node is a fast moving ship which makes finding the best method for addressing a problem difficult.

What I Want To Do
I have a java process that is doing something and then writing the results of this thing it does to a text file. It typically takes anything from 5 mins to 5 hours to run, with data being written the whole time, and can get up to some fairly hefty throughput rates (circa. 1000 lines/sec).

I would like to read this file, in real time, and then, using node aggregate the data and write it to a socket where it can be graphed on the client.

The client, graphs, sockets and aggregation logic are all done but I am confused about the best approach for reading the file.

What I Have Tried (or at least played with)
FIFO – I can tell my Java process to write to a fifo and read this using node, this is in fact how we have this currently implemted using Perl, but because everything else is running in node it makes sense to port the code over.

Unix Sockets – As above.

fs.watchFile – will this work for what we need?

fs.createReadStream – is this better than watchFile?

fs & tail -f – seems like a hack.

What, actually, is my Question
I am tending towards using Unix Sockets, this seems the fastest option. But does node have better built-in features for reading files from the fs in real time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:32:54+00:00Added an answer on June 6, 2026 at 1:32 pm

    If you want to keep the file as a persistent store of your data to prevent a loss of stream in case of a system crash or one of the members in your network of running processes dies, you can still continue on writing to a file and reading from it.

    If you do not need this file as a persistent storage of produced results from your Java process, then going with a Unix socket is much better for both the ease and also the performance.

    fs.watchFile() is not what you need because it works on file stats as filesystem reports it and since you want to read the file as it is already being written, this is not what you want.

    SHORT UPDATE: I am very sorry to realize that although I had accused fs.watchFile() for using file stats in previous paragraph, I had done the very same thing myself in my example code below! Although I had already warned readers to “take care!” because I had written it in just a few minutes without even testing well; still, it can be done better by using fs.watch() instead of watchFile or fstatSync if underlying system supports it.

    For reading/writing from a file, I have just written below for fun in my break:

    test-fs-writer.js: [You will not need this since you write file in your Java process]

    var fs = require('fs'),
        lineno=0;
    
    var stream = fs.createWriteStream('test-read-write.txt', {flags:'a'});
    
    stream.on('open', function() {
        console.log('Stream opened, will start writing in 2 secs');
        setInterval(function() { stream.write((++lineno)+' oi!\n'); }, 2000);
    });
    

    test-fs-reader.js: [Take care, this is just demonstration, check err objects!]

    var fs = require('fs'),
        bite_size = 256,
        readbytes = 0,
        file;
    
    fs.open('test-read-write.txt', 'r', function(err, fd) { file = fd; readsome(); });
    
    function readsome() {
        var stats = fs.fstatSync(file); // yes sometimes async does not make sense!
        if(stats.size<readbytes+1) {
            console.log('Hehe I am much faster than your writer..! I will sleep for a while, I deserve it!');
            setTimeout(readsome, 3000);
        }
        else {
            fs.read(file, new Buffer(bite_size), 0, bite_size, readbytes, processsome);
        }
    }
    
    function processsome(err, bytecount, buff) {
        console.log('Read', bytecount, 'and will process it now.');
    
        // Here we will process our incoming data:
            // Do whatever you need. Just be careful about not using beyond the bytecount in buff.
            console.log(buff.toString('utf-8', 0, bytecount));
    
        // So we continue reading from where we left:
        readbytes+=bytecount;
        process.nextTick(readsome);
    }
    

    You can safely avoid using nextTick and call readsome() directly instead. Since we are still working sync here, it is not necessary in any sense. I just like it. :p

    EDIT by Oliver Lloyd

    Taking the example above but extending it to read CSV data gives:

    var lastLineFeed,
        lineArray;
    function processsome(err, bytecount, buff) {
        lastLineFeed = buff.toString('utf-8', 0, bytecount).lastIndexOf('\n');
    
        if(lastLineFeed > -1){
    
            // Split the buffer by line
            lineArray = buff.toString('utf-8', 0, bytecount).slice(0,lastLineFeed).split('\n');
    
            // Then split each line by comma
            for(i=0;i<lineArray.length;i++){
                // Add read rows to an array for use elsewhere
                valueArray.push(lineArray[i].split(','));
            }   
    
            // Set a new position to read from
            readbytes+=lastLineFeed+1;
        } else {
            // No complete lines were read
            readbytes+=bytecount;
        }
        process.nextTick(readFile);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to work out the best way to approach the following problem with
I need to work out a way to create 10,000 non-repeating random numbers in
I'm looking for the best way to read data from an stdin pipe in
I am trying to work out the best approach for a data migration. I
I need to be able to work out what page is hosting my silverlight
I'm trying to work out what regular expression I would need to change this
I need to work with some old C++ code that was developed in Visual
i need to work with setTimeout function but that function does not work. First
What is the best way to store shop opening and closing time in the
I need to work out how many different instances occur on a different day,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.