I am testing the BroadcastChannel functionality and I'am having trouble. I open two Chrome windows and the dev tools for each. On the console I write:
const z = new BroadcastChannel('blarg')
z.onmessage = function (ev) {console.log(ev)}
I can examine z and it has the function saved to the onmessage prop so it all looks good. However, when I test:
z.postMessage('sweet')
in one of the consoles, nothing shows in the other. I would expect since both Chrome windows are subscribed to the broadcast channel blarg and have a function to console log the message that is posted, I would see the message sweet to be shown in the other console but nothing happens.
So two questions:
Can't I test BroadcastChannel interface in the devtools console like this?
If so, what am I missing about how BroadcastChannel works?
It works if:
blargin your case, but it could also be"")When you say
call them
AandB. In nomenclatureAandBare defined as "browsing contexts", and they could also be tabs, frames or iframes.The following code is an example that satisfies conditions 1. and 2.
devtools
A:devtools
B:Condition 1 is also satisfied when browser contexts (windows, tabs, frames or iframes) don't "point" to any regular url (for example when you press CTRL+T to open a new tab). In this case the origin has the special value of
chrome://new-tab-page.Said that, if you post from
AtoB,Bwill receive the message andconsole.logit. The same applies vice versa.