I have a large complex AngularJS Comet application ("real-time"). Clients send data to the server, and also receive notifications from the server about events other users have initiated.
In its simplest form, the app has these client-side components ("Service" below could be AngularJS .service or .factory, whatever)
[Object] Controller
: could be any object, likeStudent
,Teacher
,Lesson
,Document
[Object] Service
: holds model of the object and has methods to operate on that dataSocket Service
: wrapper for socket library
Calls can go both directions
- User Updates Something >
[Object] Controller
>[Object] Service
>Socket Service
> Server - Server >
Socket Service
>[Object] Service
>[Object] Controller
> User Notified
What is the best architecture for two-way inter-module communication in this case?
Here's what I think are the options. Please suggest other good ones if I've left them out.
- Use the circular dependency fudge. Everywhere I read it seems we shouldn't be doing that, including Lu4 in his "fudge" answer. On the other hand, doing this means you can have method calls in both directions, keeping inter-module communication consistent throughout the app.
- Use dependency injection and method calls in one direction, and events in the other direction. There's some opposition to over-using events. Is this a case of over-using?
- Use events in both directions. Moves closer to "over-use", but keeps inter-module communication consistent throughout the app.
- Use a Mediator pattern, ie have a central orchestrating service that coordinates in both directions. I'm not sure how this is different or better than using events, wherein $rootScope becomes the "mediator" against which all events and their callbacks are registered.
- Some kind of long-polling between modules, so that, eg the
[Object] Controller
makes a call to[Object] Service
, which returns a promise and in turn callsSocket Service
that also returns a promise, and whenSocket Service
receives some data, the promise resolves back up the chain, at which point[Object] Controller
initiates another request.