AngularJS and CometD

1,215 views
Skip to first unread message

Ronaldo Ferraz

unread,
Jun 5, 2012, 4:12:36 PM6/5/12
to AngularJS
Hello, folks--

I'm playing with AngularJS in a Clojure project I'm working on,
looking to replace the initial implementation I have in Backbone,
which is not working so well. I've started some tests with AngularJS
and it seems to fit the bill nicely for what I want to do.

However, I'm using CometD on the server for some of the "real-time"
stuff that I need. Looking around, I have not found anything related
to CometD and AngularJS. Is there a proper way of doing using
AngularJS, anybody using something similar or any pointers I can take
a look at? I guess I can still use the jQuery implementation I have
right now, but I rather check first.

Thanks a bunch,

R.

ronald...@gmail.com

unread,
Jun 5, 2012, 4:57:20 PM6/5/12
to ang...@googlegroups.com
Sure. CometD [1] is one the main ways push is implemented in browsers nowadays, for applications that need to transact asynchronous data between multiple parties. Most of the times, long-polling is used as the underlying implementation due to the fact that it can be easily implemented in most browsers with relative easy.

I'm using the Jetty implementation which provides a jQuery adapter [2]. Maybe the right way would be to implement a service on top of that?

[2]: http://cometd.org/documentation/cometd-javascript

On Tuesday, June 5, 2012 5:32:21 PM UTC-3, Andy Joslin wrote:
Could you give us a simple example of how CometID works?  You'll probably want to make a service called 'Comet' or the like, which receives the updates from the server.  Then the controllers can somehow subscribe to these updates using the service.

ronald...@gmail.com

unread,
Jun 6, 2012, 1:50:39 PM6/6/12
to ang...@googlegroups.com
Thanks, that's very useful, detailed info.

To answer yours questions, most events will be initiated by people using the application in groups of at most ten so I don't expect updates to happen more than a few times per minute; they will also be targeted at specific components. I guess that brings me to the best scenario you described before.

R.

On Wednesday, June 6, 2012 1:29:20 PM UTC-3, Adam Bender wrote:
I think your approach would depend on a couple things: 

1) How often are you sending updates to the client, once every ten seconds or ten times a second? 

2) Are these events global in nature or more targeted at specific components?

At a minimum you will want to create a Comet/Push service to abstract the communication away as Andy suggested. This service should be registered to start eagerly as you probably want it to run in the background even if no visual component/controller is listening. On construction this service should go through the comet handshake and prepare for incoming data, setting up error handlers etc. As a side note, this should be the only place in the codebase that ever refers to cometd, everything else in the app should be listening for events/callbacks and know nothing of the transport. 

If these messages are global in nature you should consider using $rootScope.$broadcast to notify the rest of the application of incoming data. This way any controller that wanted notification just needs to register $scope.$on and it can receive the messages. This really puts the burden of deciding how to handle the message at the level of the individual component which is a nicely decoupled approach and no one has to know if it is comet messages or something entirely different generating the data.

If however you only want to target one or two components then you can inject the push service into your controllers, though I would be cautious tying yourself specifically to Comet at the controller level. You may decide to migrate to websockets when it stabilizes and dont want to go tear up a bunch of Comet specific code.

The other thing to watch out for with Comet is that it can generate a ton of little data objects if the number of messages is really high ( >10 messages a second sustained) which can cause the GC to interrupt your UI so pay particular attention to how much data you are sending and how long you are holding on to it. Not to mention that this will cause the $scope event bus to become quite chatty, which can also have an effect on visual lag.

Misko Hevery

unread,
Jun 6, 2012, 8:07:03 PM6/6/12
to ang...@googlegroups.com
Check out angular socket.io hack I did which is essentially the same thing: https://github.com/mhevery/angular-node-socketio

remember $apply is your friend when calling into angular from outside.

-- misko

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/XDOYLRl_0jYJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

ronald...@gmail.com

unread,
Jun 7, 2012, 1:15:06 PM6/7/12
to ang...@googlegroups.com
Thanks, that helps a lot.

R.

_pa...@anotherwebsite.org

unread,
Sep 6, 2012, 2:36:51 AM9/6/12
to ang...@googlegroups.com
Hey AngularJS folks, I think I'll be needing a CometD service for Angular myself, and was wondering if anyone's published any relevant libraries or reference code that's up-to-date with a recent Angular version. (Misko's socket.io example is written against 0.9, and an actual CometD example would be better anyway if one exists.) If not, would any Angular experts or devs be willing to sketch it out? CometD is pretty established / widely used, so maybe an Angular-CometD reference would help Angular reach a bunch more people. CometD Javascript docs: http://cometd.org/documentation/cometd-javascript

Thanks for reading, and congrats on the new releases today!

_pa...@anotherwebsite.org

unread,
Sep 12, 2012, 9:58:16 AM9/12/12
to ang...@googlegroups.com, Ronaldo Ferraz
Just wanted to follow up and say Misko accepted my patch to bring https://github.com/mhevery/angular-node-socketio up-to-date with Angular 1.0, in case it might help anyone.
Reply all
Reply to author
Forward
0 new messages