MessageChannel.js proposes an implementation of the channel messaging described in the HTML5 specification.
It is important to note that the MessageChannel.js communication can only work
with browser that supports window.postMessage
.
MessageChannel.js
depends on UUID.js and Kamino.js. The easiest way to get
them is to install MessageChannel
via bower. They will then wind up in
bower_components
.
bower install MessageChannel.js
The library works almost like a polyfill.
The only difference
in the code is the way MessagePort
s are sent between user agents.
otherWindow.postMessage('hello', "http://example.com", [channel.port1]);
worker.postMessage('hello', "http://example.com", [channel.port1]);
becomes:
Window.postMessage(otherWindow, 'hello', "http://example.com", [channel.port1]);
Worker.postMessage(worker, 'hello', "http://example.com", [channel.port1]);
MessagePort
can be transfered to another user agent that isn't a parent or a child.
In that case, each intermediate user agent needs to a) persist and b) retain a handle
to the user agent it transferred the port to. So if port p is sent from agent a to c via b,
b will still see messages between a and c.MessagePort
, you'll have to encode/decode
the message with Kamino.MessageChannel.js uses Grunt to automate building and testing.
Before you use any of the commands below, make sure you have installed node.js, which includes npm, the node package manager.
If you haven't before, install the grunt
CLI tool:
$ npm install -g grunt-cli
This will put the grunt
command in your system path, allowing it to be
run from any directory.
Next, install MessageChannel's dependencies:
$ npm install
This will install all of the packages that MessageChannel's Gruntfile relies
on into the local node_modules
directory.
Run the MessageChannel tests by starting a test server:
grunt server
Once the server is running, visit http://localhost:8000/test/
in your
browser.