Web Intents

Paul Kinlan - November 2010

Source: https://github.com/PaulKinlan/WebIntents

A framework for Web-based Inter-app communication and service discovery.

How does it work?

A little bit of magic.

Actually it is pretty simple, there is a shared domain (in this case http://webintents.appspot.com/) that hosts the messaging channel. Both service and client host an IFRAME pointing to the framework and uses SharedWorkers to communicate between apps.

Sample Applications

A collection of sample applications to show you how what can be done with the Web Intents Framework.

URL Shortener

Service - The app registers its intention to provide services for the #shorten command. It receives messages from external applications and processes them returning them to the calling application.

Client - This app is a "Status posting" application, one of the functions it provides is the ability to shorten URL's using 3rd party services, the user has the option to chose one of many shortening services. Not both app and service don't know about each other. Also, check out that it integrates with "Streamie.org" (well a branch of)

Problem

On the web today applications have two methods that they can use to talk to each other. They can use:

The web has some awesome services built using these technologies, however they have a severe limitation: both apps need to know about each other. Contracts have to be arranged and services written.

Solution

This.

A framework that allows developers to register their API's for simple discovery and ease of interaction.

A system that allows developers to discover APIs and quickly integrate with them.

An ingenious solution that aides cross-document messaging in a safe and secure manner

Most Importantly, a system that allows the user to control the services that their applications interact with.

A bit more info

It's like Androids Intents, but for the Web.

The 20 second guide for Developers

For Service Builders

You have a service that you want other developers to use.

Add a script tag: <script src="http://webintents.appspot.com/lib/client.js"></script>

Initialize your service:

window.channel.initialize(
  function() {
    // Some basic meta-data about your service.
    var data = {
      "name" : "Easy Shorten - URL Shortener"
    };
    
    // Register that we are providing an intent.
    window.channel.registerIntent(
      "shorten", 
      data, 
      handleMessage // Your function
    );
  });

Add your handler for incoming messages:

var handleMessage = function(message, response) {
  // process in-coming messages.
  ...
  // If you want to return some data, you can
  if(response) {
    response({
      "data" : somedata
    });
  }
}

For Service Integrators

You have a site that uses other developers services.

Add a script tag: <script src="http://webintents.appspot.com/lib/client.js"></script>

Initialize the communication channel:

window.channel.initialize();

Query the system for the type of service you want to use:

window.channel.discover("shorten", {}, function(data) {
  // handle the list of intents that can handle this.
  var intents = data.intents;
  for(var intent in intents) {
    // Show the user a list of services they can use.
  }
}); 

Send the application some data:

window.channel.send(
  method,
  {
    "data" : anObject
  }, 
  function(data) {
    // This is the response from the app that we sent a message too.
    ...
    // Not all apps will return data.
  });