When server is idle for long (2+ hours), I have to restart the server to force it to read content of xml file.
Once restarted it works with no issue, as every time new client connects server send the content of xml to clinet.If its idle for long then it stop sending content of xml to client.
Please have a look at my code below, let me know if I am doing anything wrong.
var express = require('express'),
app = express.createServer(),
xml2js = require('xml2js'),
parser = new xml2js.Parser(),
fs = require('fs'),
var notification= 'DATA/notification.xml';
io.sockets.on('connection', function (socket) {
connected = true;
socket.on('disconnect', function (socket) {
connected = false;
});
tick(connected);
});
var tick = function(clientConnection) {
if(!clientConnection) {
return;
}
fs.exists( notification, function(exists) {
if (exists) {
fs.watch( notification, function ( curr, prev ) {
fs.readFile( notification, function (err, data ) {
if ( err ) return;
parser.parseString( data, function (err, result) {
io.sockets.emit( 'notification', result );
});
});
});
}
});
}
Cheers!!!