I get duplicate log lines when I refresh browser

161 views
Skip to first unread message

vqck

unread,
May 7, 2014, 9:54:55 PM5/7/14
to nod...@googlegroups.com
Hi,

I tail a log file on server and dipslay the log lines on the browser. The problem is: Every time I refresh my browser, I get one more duplicate log line. For example, on initial load, I get one log line on the browser for every line in the log file. If I refresh the browser, I get 2 line on the browser for every line in the log file. If I refresh again, I get 3 lines on the browser for every line in the log file. I have been looking at this for days now. Please help.


Here is my server code:
//Routing
     app.get('/', function (req, res) {
         res.redirect('/index.html');
     });
    
     server = http.createServer(app).listen(app.get('port'), function() {
       console.log('Express server listening on port ' + app.get('port'));
     });
    
     socketio = require('socket.io')
     io = socketio.listen(server)
    
     logFile = "test.log";
     io.sockets.on('connection', function(socket) {
         Tail = require('tail').Tail
         tail = new Tail(logFile);
         tail.on('line', function(data){
             return io.sockets.emit('new-data', {channel: "stdout", value: data, logFile: logFile });
         });                                                                                                                                                                                                                                
     });


Here is my client code:
         wireSocketIo: function() {
             var logPanel = Ext.ComponentQuery.query("#logPanel")[0];                                                                                                                                                                           
    
             var socket = io.connect('http://localhost:3000');
             socket.on('new-data', function(data) {    
                 var newLogLine = Ext.create("Ext.Component", {html: "<div>" + data.value + "</div>"});
                 logPanel.add(newLogLine);
             });
         },

Daniel Rinehart

unread,
May 8, 2014, 7:30:44 AM5/8/14
to nodejs
In the server every time a new socket connection is made you are creating a new Tail instance and then using io.sockets.emit() which sends a message to all connected clients, hence the behavior you are seeing. If you really just want to globally tail and emit events move that handle outside of the connection handler. If each connection is going to specify which file it wants to tail then you'll want to just emit on that connection not all connections or if you expect multiple clients to want to all tail the same file look into socket.io's room feature to only create each tail handler once. Lastly as written I don't see that the tail instances are being stopped and cleaned up when the client disconnects.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/5bf22709-9846-4cb9-bb10-04e79512bb96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages