SignalR Client Method Fired Multiple Times in Angular Service SignalR Client Method Fired Multiple Times in Angular Service angularjs angularjs

SignalR Client Method Fired Multiple Times in Angular Service


I would focus on creating the controller in the right way. For example, this kind of situations are there:

If the controller is specified in the $routeProvider and in the HTML template it will get create once for each declaration.

Services are by nature singletons, but what you really can mess around easily (as I have done, too) is in controllers. I once was on situation, where same data was fetched four times.

I would double check all controller calls from templates and routeprovider etc, everywhere you are referencing or needing one.

Having new instances on every logout may refer to a router issue. Maybe coming to root / context the a controller having the service gets initialized again and again. Then those controllers interpret every action you make in plural and mess is there.

Source: AngularJS Controller execute twice


It could well be that UserAuthenticated event is firing multiple times but it could be handled by placing a $.signalR.connectionState.disconnected check around the connection start

var initializeConnection = function(){    self.proxy = null;    var connection = $.hubConnection(ENV.socketEndpoint,{        qs: {token:User.getToken()},        logging: true,        useDefaultPath: false    });    self.proxy = connection.createHubProxy('myHub');    self.proxy.on('onMessageCreated',function(data){      $rootScope.$emit('Messages:messageReceived',data);    })    if ($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected){        connection.start().done(function(){            $log.info('connected');        })    }}

Edit: I don't actually see where you call the OnMessageCreated method?

Also have you debugged the signalR hub when calling a disconnect? I presume this gets called as normal when a user disconnects, however you also call it on $rootScope.$on('UserLoggedOut' surely your signalR hub won't be able to identify the user at this point and therefore they will remain in your Group collection? If that is the case you could expect strange behavior such as multiple calls to be raised to there residual Context.ConnectionId in the group calls.