PubSub implementation in angular

1,022 views
Skip to first unread message

Mickael Bailly

unread,
Nov 27, 2012, 11:22:39 AM11/27/12
to ang...@googlegroups.com
Hello all,

I'm currently adding some angular in a web application. This one got a simple pubsub, and I need to push the events in the angular world.

I looked at the $broadcast scope method, and it seems to me the better way to do it: because when child scopes will be removed from the dom, angular removes the event listeners, so I don't risk memory leaks. I just have to plug my existing pubsub publish method to a rootScope.$broadcast, and I'm done.
However, my current application (an audio player) emits lots of events through the pubsub (eg the audio timeupdate that happens every 200ms, the load progress, etc...) so I'm concerned by performances. I looked at the angular code and was disappointed to find that angular doesn't have some maps of "event name" => [ array of scopes listening ], but that angular goes through each and every defined scope to find event listeners...

What's your opinion about this ? Should I keep my external pubsub and play with $apply to use it angular-side, or have someone found better angular-made implementation ? Or will angular devs ready to discuss a better performance implementation of the $broadcast thing (so I can help) ?

Regards,

Mickael

Nolan Dubeau

unread,
Nov 27, 2012, 11:34:54 AM11/27/12
to ang...@googlegroups.com, ang...@googlegroups.com
I've used the the existing pubsub code (PubNub in my case) to receive the real time data and then use apply() as well as Thomas Burelsons MessagingService to broadcast the data to multiple controllers.  I've found that his approach is much more powerful than using the core Angular code for broadcast, although this depends on your requirements. There is a basic example of using apply() and PubNub on the BuiltWithAngulaJS site.  Hope this helps. The MessagingService code can be found by searching the list or Thomas' site. Good luck!

Nolan Dubeau

Sent from my Commodore-64
Load *.*,8,1
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Mickael Bailly

unread,
Nov 27, 2012, 4:22:07 PM11/27/12
to ang...@googlegroups.com
Hello Nolan,

thanks for the answer. However, looking at http://www.gridlinked.info/angularJS/modules/MessagingServices.js and http://jsfiddle.net/ThomasBurleson/sv7D5/embedded/result/ , the bad point of this implementation is that if I remove() (the jQuery/jQlite method) an HTML element that have a controller, and whose controller subscribed to the pubsub, the pubsub callback will stay, creating leaks.

For now I see two ways of doing it:

1/ I use rootScope.$broadcast() => I have performances issues
2/ I use some custom pubsub implementation, like the one you showed me, and I have to listen for a $destroy event on my controller to remove the pubsub subscriptions => not so elegant

I wanted to ask this list if someone got the plan 3, where I have performant event binding/lookup and automatic callback removal on controller destruction ?

Mickael

Ben Nadel

unread,
Nov 27, 2012, 4:48:53 PM11/27/12
to ang...@googlegroups.com
Mickael,

I recently created a PubSub lib for our AngularJS application. In order to deal with the "leak", I listen to the $destroy method on the controllers:

assume modelEvents => new PubSub();

.....

// BIND.
modelEvents.on( "userUpdated.controllerNameSpace", function( event ) { ..... } );

// UNBIND
$scope.$on( "$destroy", function() {  modelEvents.off( "userUpdated.controllerNameSpace" );  } );

Note that I name-space the events (ex. ..controllerNameSpace) in order to make the off() method easier to use (ie. no need to pass-in method reference, just name-spaced event type).

Hope that helps - listening to the $destroy event was a mental breakthrough for me as I was very nervous about leaks and re-bindings.

Cheers,
Ben Nadel
Reply all
Reply to author
Forward
0 new messages