Module: UWA/Utils/InterCom

UWA/Utils/InterCom

Cross-Platform API for cross document data exchange.

This set of Classes is used for example to dispatch events and messages accross widgets instances through an interface using events names and JSON data.

Example

// Make an alias to API, it's not required, but help for compression and shortcut.
var InterCom = UWA.Utils.InterCom;

//
// Create a new Server Instance into main page
//

var serverId = 'MyServer1';
var server = new InterCom.Server(serverId, {
    isPublic: true // Allow Socket looking for Server to connect
});

//
// SocketA
//

// Init "socketA" into an iframe or main page
var socketA = new InterCom.Socket('socketA');

// Request a Server for socketA
socketA.subscribeServer(serverId, window, 'http://...'); // no server name means that the first server with isPublic option will be matched

 // Handle "SocketAEvent" event
 socketA.addListener('SocketAEvent', function (json, info) {
     alert(json.msg); // display message from socketBData
});

//
// SocketB
//

// Init socketB into an iframe or main page
var socketB = new InterCom.Socket(); // Anonymous Socket, no body can direct dispatch on him
socketB.subscribeServer(serverId, window.parent, 'http://...');   // Specific Server

// Data will pass througt Server to other sockets
var socketBData = {msg: 'Hello from SocketB'};

//
// A. Send data to other sockets using "SocketAEvent" event and dispatchEvent function
//

// 1. Only for socketA
socketB.dispatchEvent('SocketAEvent', socketBData, 'socketA');

// 2. Only for sockets of socketB Servers
socketB.dispatchEvent('SocketAEvent', socketBData);

// 3. Only for specifics Sockets or/and Server
socketB.dispatchEvent('SocketAEvent', socketBData, ['socketA', ...], ['MyServer1',...]);

//
// B. Handle response with hooks
//

// Handle "SocketBHook" event
socketA.addListener('SocketBHook', function (json, info) {
    alert(json.responseName); // dispatch step (beforeDispatch, onDispatchLoop, afterDispatch)
    alert(json.time);         // time
});

// Then send event "SocketAEvent" and call "SocketBHook" event during server dispatch steps
socketB.dispatchEvent('SocketAEvent', socketBData, [], [], {
    beforeDispatch: 'SocketBHook',
    onDispatchLoop: 'SocketBHook',
    afterDispatch: 'SocketBHook'
});

Classes

Server
Socket

Index

Members

<static> Adapters.PostMessage :Object

Adapter for browsers that support native messaging (various implementations of the HTML5 postMessage method).

Officially defined at http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html.

postMessage is a native implementation of XDC. A page registers that it would like to receive messages by listening the the "message" event on the window object. In turn, another page can raise that event by calling window.postMessage with a string representing the message and a string indicating on which domain the receiving page must be to receive the message. The target page will then have its "message" event raised if the domain matches and can, in turn, check the origin of the message and process the data contained within.

PostMessage: postMessage on the window object.

  • Internet Explorer 8+
  • Safari 4+
  • Chrome 2+
  • Webkit nightlies
  • Firefox 3+
  • Opera 9+
Type
  • Object
Properties:
Name Type Description
isAvailable Function

Check if adapter is available.

removeListener Function

Remove event listener.

addListener Function

Add event listener.

dispatchEvent Function

Dispatch event.

<static> Adapters.FrameCallback :Object

Adapter using IFrame URL Technique Officially defined at http://msdn.microsoft.com/en-us/library/bb735305.aspx.

For all others, we have a fallback mechanism known as "FrameCallback". FrameCallback exploits the fact that while same-origin policy prohibits a frame from accessing members on a window not in the same domain, that frame can, however, navigate the window heirarchy (via parent). This is exploited by having a page on domain A that wants to talk to domain B create an iframe on domain B pointing to a special relay file and with a message encoded after the hash (#). This relay, in turn, finds the page on domain B, and can call a receipt function with the message given to it. The relay URL used by each caller is http://example.com/intercom.html and must be present before the call method is used.

FrameCallback: Iframe-based method, utilizing a relay page, to send a message.

  • No known major browsers still use this method, but it remains useful as a catch-all fallback for the time being.
  • Internet Explorer 5+
  • Safari 1+
  • Chrome 1+
  • Webkit nightlies
  • Firefox 1+
  • Opera 5+
Type
  • Object
Properties:
Name Type Description
isAvailable Function

Check if adapter is available.

removeListener Function

Remove event listener.

addListener Function

Add event listener.

dispatchEvent Function

Dispatch event.

Methods

<static> getAdapter()

Get InterCom Adapater instance.