When server is idle for long, I have to restart the server to force it to read content of xml file.

37 views
Skip to first unread message

Suraj S Thapa

unread,
May 6, 2013, 2:00:12 AM5/6/13
to sock...@googlegroups.com
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(),
    socket = require('socket.io'),
    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!!!

Reply all
Reply to author
Forward
0 new messages