Simple tcp server that watches file changes and notifies clients about em.

'use strict';

const

	fs = require('fs'),
	net = require('net'),

	filename = process.argv[2],

	server = net.createServer((connection) => {
		console.log('Subscriber connected.');

		connection.write(`Now watching '${filename}' for changes... \n`);

		let watcher = fs.watch(filename, () => {
			connection.write(`File '${filename}' changed: ${Date.now()}\n`);
		});
		
		connection.on('close', () => {
			console.log('Subscriber disconnected.');
			watcher.close();
		});

	});

if (!filename) {
	throw new Error('No target filename was specified.');
}

server.listen(5432, () => {
	console.log('Listening for subscribers');
});

Runs like this:

node net-watcher.js target.txt

Can be connected via telnet:

telnet localhost 5432

Touch watched file to see some results:

touch target.txt

Unix socket

The same app using unix socket:

'use strict';

const

	fs = require('fs'),
	net = require('net'),

	filename = process.argv[2],

	server = net.createServer((connection) => {
		console.log('Subscriber connected.');

		connection.write(`Now watching '${filename}' for changes... \n`);

		let watcher = fs.watch(filename, () => {
			connection.write(`File '${filename}' changed: ${Date.now()}\n`);
		});
		
		connection.on('close', () => {
			console.log('Subscriber disconnected.');
			watcher.close();
		});

	});

if (!filename) {
	throw new Error('No target filename was specified.');
}

server.listen('/tmp/watcher.sock', () => {
	console.log('Listening for subscribers');
});

To connect the app:

nc -U /tmp/watcher.sock