Events between services, or any other way to avioid own publish/subscribe

1,825 views
Skip to first unread message

Tomek Kos

unread,
Dec 18, 2012, 3:14:23 AM12/18/12
to ang...@googlegroups.com
Hi,

I wonder if there is an Angular-Way of doing communication between services. 
What I'd like to do is to have one service that produces messages somehow (say it wraps kind of realtime messaging like PubSub) and I want it to update other service data, but I don't want to have direct dependency as there may be several services interested in being notified that message was received. 
What I have for now is reinventing the wheel of observe/notify and play with function "call". You can see it here http://jsfiddle.net/canree/dD5VR/
Could you please advise how I could improve all that stuff?

Pawel Kozlowski

unread,
Dec 18, 2012, 3:19:16 AM12/18/12
to ang...@googlegroups.com
Hi!

On Tue, Dec 18, 2012 at 9:14 AM, Tomek Kos <can...@gmail.com> wrote:
> I wonder if there is an Angular-Way of doing communication between services.

Probably the easiest, the most straightforward way is just to inject
one service into another.
This is where AngularJS dependency injection shines and allows you to
have loosely coupled services work together.
As an example you might want to have a look at the larger sample app
where a sample notification service is injected with a localization
service:
https://github.com/angular-app/angular-app/blob/master/client/src/common/services/i18nNotifications.js#L2

Cheers,
Pawel

--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Witold Szczerba

unread,
Dec 18, 2012, 3:35:14 AM12/18/12
to ang...@googlegroups.com

Hi,
Use $rootScope for this. One service send $broadcast, all other listen using $on method.
I always prefix message names e.g. "event:" so it's easy to find all the usages by doing text search in code.

Regards,
Witold Szczerba
---
Sent from my mobile phone.

--
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.
 
 

Tomek Kos

unread,
Dec 18, 2012, 5:01:08 AM12/18/12
to ang...@googlegroups.com

Thanks guys for your inputs.

Witold, you mean something like this? http://jsfiddle.net/canree/dD5VR/2/
I removed dependencies between services and left only $rootScope with $broadcast and $on. 

Witold Szczerba

unread,
Dec 18, 2012, 5:50:19 AM12/18/12
to ang...@googlegroups.com
Hi,
yes, that could look like this, but with one (but important) correction:
http://jsfiddle.net/witoldsz/dD5VR/4/
I am assuming your PubSubFake equivalent will never fire from withing
AngularJS call stack.

Also remember one thing: $rootScope is a singleton. Once you subscribe
to it using $on - it will stay there until the end of application, so
never subscribe to it from object with shorter life cycle. For
example, in controllers you subscribe to their associate $scope
instance (but you can broadcast from root), so all those listeners
ends when scope gets killed. Same thing applies to directives. If you
have one which can live shorter than the scope it uses - make it use
its own one or your $on listeners will start accumulating.

Regards,
Witold Szczerba

Tomek Kos

unread,
Dec 18, 2012, 6:20:24 AM12/18/12
to ang...@googlegroups.com
Hmm. Both ways (yours and mine) seem to work the same way. Why do you need to wrap init into $rootScope.$apply? Why is it important? 
Sorry for asking noob questions, but I'm trying to understand that for future use?

Witold Szczerba

unread,
Dec 18, 2012, 7:07:17 AM12/18/12
to ang...@googlegroups.com
Hi again,
read the Developer Guide, initial section describes this:
http://docs.angularjs.org/guide/concepts

Please people, read the official documentation and only after then
start to code :)

Regards,
Witold Szczerba

jmeco

unread,
Dec 20, 2012, 7:08:12 AM12/20/12
to ang...@googlegroups.com

John Fletcher

unread,
Dec 26, 2012, 4:22:09 AM12/26/12
to ang...@googlegroups.com
I'm a beginner with Angular, and I got this going for now by having a modelService (which is basically my model). Listeners watch the lastMessage property of this service, and publishers modify this property. Of course you can diversify the amount of properties you watch like this. It seems to work. Is this crazy?
 
John


2012/12/20 jmeco <jmay...@eco2market.com>
--

Witold Szczerba

unread,
Dec 26, 2012, 3:07:16 PM12/26/12
to ang...@googlegroups.com

I don't think it's crazy. Sounds like a good solution.
Most important is that your service does not handle listeners, so you do not have to worry about leaks.

Regards,
Witold Szczerba
---
Sent from my mobile phone.

John Fletcher

unread,
Dec 28, 2012, 4:08:29 AM12/28/12
to ang...@googlegroups.com
OK, thanks, that gives me some peace :-).


2012/12/26 Witold Szczerba <pljos...@gmail.com>
Reply all
Reply to author
Forward
0 new messages