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 IRC (such as one of these permanent autoconfirmed members).

Difference between revisions of "RemoteDocumentMessaging"

From WHATWG Wiki
Jump to: navigation, search
 
(8 intermediate revisions by one other user not shown)
Line 10: Line 10:
  
 
<pre>
 
<pre>
partial interface Window {
+
[Constructor(DOMString name)]
     MessagePort? connectTo(DOMString target);
+
interface BroadcastChannel : EventTarget {
 +
     void postMessage(any message);
 +
    void close();
 +
 
 +
    attribute EventHandler onmessage;
 
};
 
};
 
</pre>
 
</pre>
 
The <code>connectTo</code> method opens a message port to 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).
 
  
 
==Examples==
 
==Examples==
  
===Listener===
+
<pre>
 +
// The channel name, "deep-thoughts", is scoped to the current origin.
 +
var channel = new BroadcastChannel("deep-thoughts");
  
<pre>
+
channel.addEventListener("message", function(message) {
window.addEventListener("connect", function(message) {
+
   // Do something interesting with |message|, which is a MessageEvent.
   // 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("Welcome!");
 
  // You can call remote.addEventListener("message", ... ) to continue communicating with the remote window.
 
 
}, false);
 
}, false);
  
// Naming your window makes you discoverable by other documents in your origin.
+
// Send a message to each other BroadcastChannel instance with the same channel
window.name = "the-listener";
+
// name in the same origin.
 +
channel.postMessage("I think, therefore I am.");
 
</pre>
 
</pre>
  
===Sender===
+
==Notes==
  
<pre>
+
You can already accomplish this through localStorage as that broadcasts storage events: http://bens.me.uk/2013/localstorage-inter-window-messaging Something that does not involve synchronous IO would probably be better though.
var port = window.connectTo("the-listener");
 
 
 
port.addEventListener("message", function(message) {
 
  if (message.data == "Welcome!")
 
    alert("Success!);
 
}, false);
 
 
 
port.start();
 
</pre>
 
  
 
[[Category:Proposals]]
 
[[Category:Proposals]]

Latest revision as of 03:20, 23 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

[Constructor(DOMString name)]
interface BroadcastChannel : EventTarget {
    void postMessage(any message);
    void close();

    attribute EventHandler onmessage;
};

Examples

// The channel name, "deep-thoughts", is scoped to the current origin.
var channel = new BroadcastChannel("deep-thoughts");

channel.addEventListener("message", function(message) {
  // Do something interesting with |message|, which is a MessageEvent.
}, false);

// Send a message to each other BroadcastChannel instance with the same channel
// name in the same origin.
channel.postMessage("I think, therefore I am.");

Notes

You can already accomplish this through localStorage as that broadcasts storage events: http://bens.me.uk/2013/localstorage-inter-window-messaging Something that does not involve synchronous IO would probably be better though.