This is rather simple to imagine, but I haven\'t found any resources mentioning what is correct approach to this issue.
I\'d like to broadcast event in one angular
I will begin by describing the aspect of scales of an application in order to provide clarity in what to implement to "properly" achieve this.
First, You have an application that is running on a server, contains a core which encapsulates modules. The lower-level you go from here could consist of either more modules or controllers. I will address both.
Prerequisites include but are not limited too:
If you need a communication-network between your controllers, use the standard Angular $scope
& $rootScope
.$broadcast
, .$emit
, and .$on
utilities inside your module(s) -- you probably already thought of this ;)
To communicate between modules, implement the Mediator Pattern in your core -- probably, this will be implemented as a Service that each module can pull in; otherwise, your modules can be initialized & provided a sandbox with Mediator/Director injected. Now your modules within your application-core can communicate.
Say, you need this application / core to communicate with another application. Implement a SharedWorker with custom events. You can see a framework, "worker.io" I built for UCLA here. The project could use some touching up, so feel free to use it as a reference to write your own -- the point to focus on is that it uses Pseudo Port-Entanglement. This is because workers only allow an onmessage
handler, so you want to create a new MessageEvent
; send it as a JSON String to the other port, and then JSON.parse` the message to get the custom event and dispatch that event on the port that received the event -- this gives you custom events. Now, any application can utilize this SharedWorker and communicate to other apps through it -- it even allows for communication between browser tabs.
If you want a more global approach to an Event-Driven Architecture, say, to give modules on the server a chance to respond to events (from anywhere) -- implement an event-hub on your server. This can be more of a project than you'd like to embark on, but a great book exists which describes how to set this up: Testable JavaScript, Mark Ethan Trostler.
That said, it is completely possible to elegantly implement a highly-optimized architecture for Event-Based Systems using Mediators, SharedWorkers, and EventHubs on the back-end.
The worker.io framework noted above uses two libraries: Apis (Bee in latin), and Scriptorium (Hive in latin). However, to see how to implement the libraries, see the js directory.
Hope this helps.