I want to realise a communication (by sending and receiving messages) between two services in an angularjs web site. Actually, messaging would be unnecessary if we could merge two services into one service. But the reality is I don't want to merge them for some reason. As mutual dependency of two services is not possible (please correct me if i am wrong), it seems that I have to let them communicate via messaging.
Let me explain... Assume we have two services serA
and serB
. In serA
, we have
this.startTask1 = function () {
console.log("B, please do task 1")
sendMsgFromAtoB("B, please do task 1")
}
this.receiveMsgFromBtoA = function (msg) {
if (msg === "A, B has done task 1") {
console.log("A, B has done task 1");
console.log("task 1 is finally over!!!")
}
}
in serB
, we have
this.receiveMsgFromAtoB = function (msg) {
if (msg === "B, please do task 1") {
console.log("B is doing task 1... Done!");
sendMsgFromBtoA("A, B has done task 1")
}
}
As a result, if a controller runs serA.startTask1()
, I expect to see in the console:
B, please do task 1
B is doing task 1... Done!
A, B has done task 1
task 1 is finally over!!!
To this end, I need to make sendMsgFromAtoB
and sendMsgFromBtoA
, and complete receiveMsgFromAtoB
and receiveMsgFromBtoA
. However, I don't know by which mean I could implement them.
I have used $window.addEventListener("message", ...)
(ie, postMessage
) and $window.addEventListener("storage", ...)
(ie, StorageEvent
). But StorageEvent
is not fully cross-platform, I am not sure postMessage
is good for messaging inside 1 page.
So does anyone have a good idea?