8

I am monitoring the file for content change using Node.js watch File. Its succesfully calling the event when content of the file is modified

fs.watchFile(filePath, ()=> {
    console.log('File Changed ...');
    file = fs.readFileSync(filePath);
    console.log('File content at : ' + new Date() + ' is \n' + file);
});

from Hello to Hello World

I want only the World not all the contents of the file, can any one suggest the most efficient way to achieve this or any node package to achieve this. I looked at node package Chokidar but that also monitors the change.

I found a JAVA Solution for that, but not sure of any Node.js alternative for that.

How to watch file for new content and retrieve that content

I did my research tried to follow these posts

https://nodejs.org/api/fs.html#fs_fs_watchfile_filename_options_listener

http://www.codingdefined.com/2015/09/how-to-monitor-file-for-modifications.html

Observe file changes with node.js

Thanks

Community
  • 1
  • 1
Akash
  • 131
  • 1
  • 5
  • it's not possible at low level, you'll have to capture file content beforehand and do your own diffing on change - similar to what your Java example does – Andrey Sidorov Jan 27 '17 at 05:54
  • Thanks @AndreySidorov, will in that case I will dig more into this and to try to find efficient algorithms to achieve that. – Akash Jan 27 '17 at 14:54

1 Answers1

6

The best way to do it is by creating a stream that reads the file and remains open for file changes. I found this tool that describes and solves this problem:

tailing-stream is a Node.js module that provides a Stream that can read continuously from a file that's being actively written to. This is in contrast to the standard fs.createReadStream. method, which returns a ReadableStream that stops reading once it gets to the last byte that existed at the time the stream was originally opened. It supports exactly the same interface as a Node ReadableStream, and its createReadStream method functions the same as fs.createReadStream.

https://github.com/jasontbradshaw/tailing-stream

You can install it using npm:

npm install tailing-stream

And here's a simple example:

const fs = require('fs');
const TailingReadableStream = require('tailing-stream');

const stream = TailingReadableStream.createReadStream("file", {timeout: 0});

stream.on('data', buffer => {
  console.log(buffer.toString());
});
stream.on('close', () => {
  console.log("close");
});
Hendry
  • 882
  • 1
  • 11
  • 27
Hugo Aboud
  • 443
  • 3
  • 10
  • I tried using tailing-stream exactly like your example to read a file that is constantly being written by another process, it is reading, but just when I right click the file on explorer. Apparently it is detecting the file have changed just when I do that. How to fix this issue? – Bodosko May 07 '20 at 12:51
  • That's pretty strange. Which OS are you using? Have you found anything else that would trigger the tailing-stream aside from right clicking the file? Be aware that Tailing Stream only supports additive modifications to the file. – Hugo Aboud May 08 '20 at 02:45
  • Im using Windows 10. I found that if I try to fs.write to this file, its denied because resource busy error, and this strangely makes tailing-stream trigger. So to my on data event actually keeps triggering, I had to set an interval that keeps trying to fs.write the file. This whole issue also occurs if I use any other “file content watcher” – Bodosko May 09 '20 at 12:43