angular-socket-io icon indicating copy to clipboard operation
angular-socket-io copied to clipboard

socket.removeListener not working

Open swapnil001 opened this issue 9 years ago • 4 comments

Hi,

I am trying to remove one listener when my controller is changing via

$scope.$on('$destroy', function() {
            hpSocketFactory.removeListener(emiterListernerLeagueLobby, function(err) {
            });
            hpSDSocketFactory.removeListener(emiterListernerMatchID, function(err) {

            });
        });

But it does not seem to work. Listener is not removed. How to remove one particular listener when controller is changing?

swapnil001 avatar Dec 16 '16 09:12 swapnil001

I'm experiencing the same issue. Did you find a solution?

melliott03 avatar Jan 15 '17 16:01 melliott03

No. It did not work. I had to change my system to work with removeAllListener property and on controller exit remove all listeners.

swapnil001 avatar Jan 15 '17 18:01 swapnil001

I came up with the following solution to make removeListener work if you're interested. If involves not angular-socket-io and writing a factory to export socket.io. I think it's easier and more straightforward and it works.

melliott03 avatar Jan 16 '17 23:01 melliott03

  1. include the cdn in your html
  1. Create a factory
myApp.factory('Socket', ['$rootScope', function ($rootScope) {
  var socket = io.connect();
  return {
    on: function (eventName, callback) {

      function wrapper() {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      }

      socket.on(eventName, wrapper);

      return function () {
        socket.removeListener(eventName, wrapper);
      };
    },

    emit: function (eventName, data, callback) {

      socket.emit(eventName, data, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          if(callback) {
            callback.apply(socket, args);
          }
        });
      });
    },

    off: function (eventName) {

      function wrapper() {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      }

      socket.off(eventName);

      return function () {
        socket.removeListener(eventName, wrapper);
      };
    },

    removeAllListeners: function (eventName) {
      socket.removeAllListeners(eventName);

      return function () {
        socket.removeAllListeners(eventName);
      };
    },

    addListener: function (eventName, callback) {
      function wrapper() {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      }

      socket.on(eventName, wrapper);

      return function () {
        socket.on(eventName, wrapper);
      };
    },

  };
}]);
  1. Inject the factory into your controller
  2. instead of socket.removeListener, use socket.off(eventName)
  3. uninstall angular-socket-io

melliott03 avatar Jan 16 '17 23:01 melliott03