M
M
Mikhailo Poberezhny2016-04-11 14:37:41
Angular
Mikhailo Poberezhny, 2016-04-11 14:37:41

Issue with socket.io on client?

I have a controller:
Client code:

.controller('chatNotificationCtrl', ['$scope', 'socket', 'toastr', '$http', function($scope, socket, toastr, $http) {

  $scope.chatNotifications = {};

  if ($scope.logged) {
    $scope.chatNotifications = $scope.logged.chatNotify;
  } else {
    return;
  }
  socket.on('notify:chat', function(data) {
    console.log('NOTIFY CHAT');
    $scope.chatNotifications.unshift(data);
    toastr.success('Example', 'New Message');
  });
  $scope.$on('$destroy', function() {
    socket.removeAllListeners();
  });
}]);

the problem is that when i send emit to "notify:chat'" on my client sometimes "on" is duplicated
for this event how to fix it? I understand that this is happening due to the restart of the controller and
new listeners are created but the old ones are not deleted. Help :(
Код сервера:

                io.sockets.to(message.users).emit('notify:chat', newChatNotify);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alastor, 2016-04-11
@Alastor

try inside removeAllListeners to specify which listeners you are removing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question