How to avoid duplicate events in socket.io when you are working with multiple tabs?
<p>When i receive message from server <code>ditConsumer</code> based on flag i am invoking a function <code>sendMessageToFile()</code> that is working as expected, problem here is when i navigate to other page and come back again so here when message comes in socket.on is listening twice and executing all variables and method twice. if i go back and forth three time it will listen 3 times, I would appreciate help here, it looks like socket.io connection problem over multiple tabs.</p>

<p>ctrll.js</p>

<pre><code>      socket.on('ditConsumer',function (data) {
            console.log('SEND MESSAGE FLAG',sendMessageFlag)
            console.log('MESSAGE FROM SERVER',data);
            var obj = {
                file:$scope.filename,
                data:data
            }
            $scope.event.push(data);
           // socket.emit('messageToFile',obj);
            if(sendMessageFlag === true) {
               return  sendMessageToFile(obj);
            }
        });

   function sendMessageToFile (data){
         if(data.file) {
             socket.emit('startrecording', data);
             $scope.disabledRecBtn = true;
             $scope.disabledStopBtn = false;
             $scope.showMessage = true;
         }
    }
</code></pre>

<p>socketFactory.js</p>

<pre><code>angular.module('loggingApp').factory('socket', function($rootScope) {
    'use strict';
    var server = 'http://localhost:3000';
    var socket = io.connect(server, {
        'forceNew': true
    });
    return {
        on: function(eventName, callback) {
            socket.on(eventName, function() {
                var args = arguments;
                $rootScope.$apply(function() {
                    callback.apply(socket, args);
                });
            });
        },
        emit: function(eventName, data, callback) {
            socket.emit(eventName, data, function() {
                var args = arguments;
                $rootScope.$apply(function() {
                    if (callback) {
                        callback.apply(socket, args);
                    }
                });
            })
        }
    };

});
</code></pre>

<p>serverIo.js</p>

<pre><code>var sio = require('socket.io');
var ditconsumer = require('./consumers/ditconsumer');
var logsRecording = require('./records/logsRecording');
var io = null;
exports.io = function () {
    return io;
};
exports.initialize = function(server) {
    io = sio(server);
    io.on('connection', function(socket) {
        // Producer.startProducer();
        ditconsumer.start(function(value){
         io.emit('ditConsumer',value);
         });
        socket.on('createlogfile', function(params) {
            logsRecording.userLogs(function(err,filename) {
                if (err){
                    console.log(err);
                } else {
                    socket.emit('filename', filename);
                }
            });

        });
        socket.on('startrecording', function(obj) {
            logsRecording.recordLogs(obj);
        });
        socket.on('stopRecording',function (filename) {
            console.log('stop recording data',filename);
            logsRecording.deleteFile(filename);
        });
    });
};
</code></pre>