<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>techno.blog(&#34;Dion&#34;) &#187; messaging</title>
	<atom:link href="http://almaer.com/blog/tag/messaging/feed" rel="self" type="application/rss+xml" />
	<link>http://almaer.com/blog</link>
	<description>blogging about life, the universe, and everything tech</description>
	<lastBuildDate>Fri, 25 May 2012 16:56:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Gears Future APIs: Messaging API</title>
		<link>http://almaer.com/blog/gears-future-apis-messaging-api</link>
		<comments>http://almaer.com/blog/gears-future-apis-messaging-api#comments</comments>
		<pubDate>Thu, 27 Dec 2007 15:47:38 +0000</pubDate>
		<dc:creator>dion</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Gears]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[apis]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[messaging]]></category>

		<guid isPermaLink="false">http://almaer.com/blog/gears-future-apis-messaging-api</guid>
		<description><![CDATA[Once you start delving into the WorkerPool API, you quickly see how a common pattern would be using it as a messaging system. As it stands, it looks like an Open Web version of Erlang processes.
Scott Hess wrote up his thoughts on a more formal Messaging API based on WorkerPool:

Gears WorkerPool has two pieces, the [...]]]></description>
			<content:encoded><![CDATA[<p>Once you start delving into the <a href="http://code.google.com/apis/gears/api_workerpool.html">WorkerPool API</a>, you quickly see how a common pattern would be using it as a messaging system. As it stands, it looks like an Open Web version of Erlang processes.</p>
<p>Scott Hess wrote up his thoughts on a more formal <a href="http://code.google.com/p/google-gears/wiki/MessagingAPI">Messaging API</a> based on WorkerPool:</p>
<blockquote><p>
Gears WorkerPool has two pieces, the part about running a bit of JS asynchronously, and the part about trading messages with that JS. This API may be composable from more basic bits. The messaging bit could be used in other contexts, such as implementing something like <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/section-crossDocumentMessages.html">WhatWG&#8217;s postMessage()</a>.
</p></blockquote>
<p><b>Aside: What is WhatWG&#8217;s postMessage?</b></p>
<p><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/section-crossDocumentMessages.html">postMessage</a> is &#8220;a messaging system that allows documents to communicate with each other regardless of their source domain, in a way designed to not enable cross-site scripting attacks.&#8221;</p>
<p>Here is an example:</p>
<blockquote><p>
For example, if document A contains an <code><a href="section-embedded0.html#object">object</a></code> element that contains document B, and<br />
    script in document A calls <code title="dom-window-postMessage"><a href="#postmessage">postMessage()</a></code> on document B, then a<br />
    message event will be fired on that element, marked as originating from<br />
    document A. The script in document A might look like:</p>
<pre>var o = document.getElementsByTagName('object')[0];
o.<span title="dom-object-contentWindow">contentWindow</span>.<a href="#postmessage" title="dom-window-postMessage">postMessage</a>('Hello world');
</pre>
<p>To register an event handler for incoming events, the script would use<br />
    <code title="">addEventListener()</code> (or similar mechanisms). For<br />
    example, the script in document B might look like:</p>
<pre>document.addEventListener('message', receiver, false);
function receiver(e) {
  if (e.domain == 'example.com') {
    if (e.data == 'Hello world') {
      e.source.postMessage('Hello');
    } else {
      alert(e.data);
    }
  }
}</pre>
<p>This script first checks the domain is the expected domain, and then<br />
    looks at the message, which it either displays to the user, or responds<br />
    to by sending a message back to the document which sent the message in<br />
    the first place.</p>
</blockquote>
<p><b>Back to the Gears Messaging API</b></p>
<p>Scott gives an example of the messaging API, starting with an end point:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> port <span style="color: #339933;">=</span> google.<span style="color: #660066;">gears</span>.<span style="color: #660066;">factory</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'beta.messageport'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1.0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
port.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>port<span style="color: #339933;">,</span> msg<span style="color: #339933;">,</span> sender<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;message: &quot;</span> <span style="color: #339933;">+</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
port.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// Omit for anonymous listener.</span></pre></div></div>

<p>and having a way to send it a message:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> port <span style="color: #339933;">=</span> google.<span style="color: #660066;">gears</span>.<span style="color: #660066;">factory</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'beta.messageport'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'1.0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
port.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
port.<span style="color: #660066;">sendMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hello there&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To enable cross domain, you can <code>post.open(name, domain)</code>, and on the other side, you have to allow it via something like <code>port.allowCrossOrigin(["www.good.com", "www.angelic.com"]);</code>.</p>
<p>I am excited about a messaging API, as I think that it fits into the way in which we are developing new Web applications. Having an asynchronous queue that allows me to replay work (e.g. offline), and work nicely with Comet based interactions, would be great. We can reuse all that we have learned from other event based systems, and <a href="http://www.enterpriseintegrationpatterns.com/ramblings.html">Gregor</a> can rename his book and be happy!</p>
<p><b>Other Future APIs</b></p>
<ul>
<li><a href="http://almaer.com/blog/gears-future-apis-location-api">Location API</a></li>
<li><a href="http://almaer.com/blog/gears-future-apis-desktop-shortcut-api">Desktop Shortcut API</a></li>
<li><a href="http://almaer.com/blog/gears-future-apis-image-manipulation-api">Image Manipulation API</a></li>
<li><a href="http://almaer.com/blog/google-gears-upgrading-from-a-1950s-chevy-in-cuba">Introduction to the series</a></li>
</ul>
<p><em>Disclaimer: This is early days, and who knows what the final API will look like, or if it will even make it. Do you have ideas for cool Gears that make the Web better? <a href="http://groups.google.com/group/google-gears/">Let us know!</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://almaer.com/blog/gears-future-apis-messaging-api/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

