A user account is required in order to edit this wiki, but we've had to disable public user registrations due to spam.

To request an account, ask an autoconfirmed user on Chat (such as one of these permanent autoconfirmed members).

RemoteDocumentMessaging: Difference between revisions

From WHATWG Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:


The user opens the same word processing document hosted by a document editing web site in two different browser windows.  The document editing web site wants to ensure that edits in one window a reflected in the other window without needing to round-trip through the server (e.g., because the user is offline).  Unfortunately, the documents are in different units of related browsing contexts, which means the two documents cannot form a direct script connection.  Using remote document messaging, the two documents can discover each other and update each other about edits performed by the user.
The user opens the same word processing document hosted by a document editing web site in two different browser windows.  The document editing web site wants to ensure that edits in one window a reflected in the other window without needing to round-trip through the server (e.g., because the user is offline).  Unfortunately, the documents are in different units of related browsing contexts, which means the two documents cannot form a direct script connection.  Using remote document messaging, the two documents can discover each other and update each other about edits performed by the user.
==Specification==
<pre>
partial interface Window {
    MessagePort? connectTo(DOMString target);
};
</pre>
The <code>connectTo</code> method instructs the user agent to search for a browsing context with name <code>target</code> within the caller's origin (although not necessarily within the caller's unit of related browsing contexts).
If the user agent finds such a browsing context, the user agent creates two entangled message ports.  The first port is returned to the caller ...


==Examples==
==Examples==
Line 31: Line 43:
<pre>
<pre>
var remote = window.connectTo("the-listener");
var remote = window.connectTo("the-listener");
if (!remote)
 
  return; // Couldn't find a remote window with the proper name in this origin.
remote.addEventListener("message", function(message) {
remote.addEventListener("message", function(message) {
   if (message.data == "Thanks for the message!")
   if (message.data == "Thanks for the message!")

Revision as of 18:22, 21 June 2013

Overview

This document describes a proposal for discovering and communicating with "remote" documents. In particular, this proposal lets a web page communicate with another document even if the web page does not have a direct script connection to that document.

Use Cases

The user opens the same word processing document hosted by a document editing web site in two different browser windows. The document editing web site wants to ensure that edits in one window a reflected in the other window without needing to round-trip through the server (e.g., because the user is offline). Unfortunately, the documents are in different units of related browsing contexts, which means the two documents cannot form a direct script connection. Using remote document messaging, the two documents can discover each other and update each other about edits performed by the user.

Specification

partial interface Window {
    MessagePort? connectTo(DOMString target);
};

The connectTo method instructs the user agent to search for a browsing context with name target within the caller's origin (although not necessarily within the caller's unit of related browsing contexts).

If the user agent finds such a browsing context, the user agent creates two entangled message ports. The first port is returned to the caller ...

Examples

Listener

window.addEventListener("message", function(message) {
  if (message.origin != location.origin)
    return;
  if (message.data != "Hi there!")
    return;
  // message.source is a MessagePort we can use to talk to the remote message, just like in a shared worker.
  var remote = message.source;
  remote.postMessage("Thanks for the message!");
  // You can call remote.addEventListener("message", ... ) to continue communicating with the remote window.
}, false);

// Naming your window makes you discoverable by other documents in your origin.
window.name = "the-listener";

Sender

var remote = window.connectTo("the-listener");

remote.addEventListener("message", function(message) {
  if (message.data == "Thanks for the message!")
    alert("Success!);
}, false);

remote.postMessage("Hi there!");