9 Feb 19:15
connection ceremony for iframe postMessage communications
John J Barton <johnjbarton <at> johnjbarton.com>
2012-02-09 18:15:28 GMT
2012-02-09 18:15:28 GMT
Recently I've been working with iframe messaging. The postMessage
solution has a lot of advantages and good traction across iframes,
WebWorkers, and browser extensions, with lots of overlap with Web
Sockets.
However the technology has two significant problems. First is the
"contentWindow that is not a window" confusion I discussed recently.
Second concerns the connection setup. I describe the second problem
here.
The basic communications solution is simple enough:
window.addEventListener('message', handler, false); // I'm listening!
portToOtherWindow.postMessage(message); // I'm talking to you!
However the solution has two significant problems:
1. There is no way to know if portToOtherWindow is connected before
you issue postMessage()
2. All iframes send messages to the same "handler".
The first problem arises because web apps are increasingly
asynchronous for load performance and other reasons.
This leads developers to look for events that will tell them about
'load' on iframes, and that leads them to try
iframe.contentWindow.addEventListener(). It works fine for same-domain
iframes, but fails for cross-domain.
The second problem arises because the handler is attached to the
window and not to an object related to the connection between the two
windows.
(Continue reading)
"Why should the connectivity part be part of the platform?"
To simplify and thus encourage cross-domain application development as
a natural and powerful extension of the Web.
Multiple incompatible connection sequences exist and will naturally
arise. One may eventually dominate. The process will be long and
painful; the advantages of competition seem low. By pursuing a
standard connection we accelerate the process.
A different push-back would make sense to me: it's not yet time. By
having this discussion we put the possibility on the table. Already
I've learned how to use the MessageChannel solution and that
RSS Feed