2

is it a good option to use $rootScope.emit methhod to propagate communicate from websocket? are they any distinct disadvantages of that? I have websocket connection in my app and I use also $http service. I'm pondering if propagating server respons via emit is a good option in general for both to unify the communication a little bit.

speedingdeer
  • 1,236
  • 2
  • 16
  • 26

1 Answers1

3

First take note that $rootScope.$broadcast and $rootScope.$emit both broadcast the events in different directions through the $scope tree, so make sure you are using the right one. In your case I would use $rootScope.$broadcast.

As for eventing out the results of your socket and http communications, the advantage would be that multiple components would all be able to easily react to those events by adding $on listeners for the events you are broadcasting. That is a good idea if you have that need for multiple components to listen. The cost to doing it that way is in the additional overhead the eventing will incur. It is not significant, but its there. In the case of web sockets, you could end up with a lot of events propagating through the $scope tree if the socket sever is very chatty. I would suggest that if a controller or service is the only one that needs to talk to web services, then it should do so directly and then simply update $scope properties so that any components already bound to the scope will get the change. If your use case is more complex then it may indeed warrant the eventing solution.

In my own angular webapp which utilizes websockets quite heavily, I opted to create a service to wrap the websocket communication, and then interested components can inject that service and register to receive only the messages they are interested in. This way I avoid running a lot of uneccessary eventing traffic through the entire $scope tree because of a very chatty websocket server (in my specific case).

gregtzar
  • 2,458
  • 3
  • 26
  • 31
  • I like very much you answer it's very complete. Thank you for that. This tread says that emit doesn't broadcast through the tree http://stackoverflow.com/questions/11252780/whats-the-correct-way-to-communicate-between-controllers-in-angularjs I'm confused now so I need to check and I'm closing the discussion yet – speedingdeer Nov 27 '13 at 22:41
  • It is true that when used from $rootScope, $emit won't bubble through the tree because it travels in the wrong direction, which is why I said to use $broadcast, because I figured if you used $emit on $rootScope then none of your listeners would ever fire. However, if you try it and they do fire and what that thread says is true then you should be able to get around any performance snags by using $rootScope.$emit and could feel confident using it for all your websocket messaging like you planned. – gregtzar Nov 28 '13 at 01:29
  • Thanks Egg, I will try to follow thiese hints – speedingdeer Nov 28 '13 at 11:34
  • 6
    Yep, please use `$rootScope.$emit`. I see `$rootScope.$broadcast` as an anti pattern. Whenever you want to make an event globally, there's no need to push it downwards through all scopes. Just $emit it directly on the $rootScope and subscribe to it directly on the `$rootScope` and everything will be fine. – Christoph Dec 07 '13 at 00:50