Embedded Experiences Tutorial

Table Of Contents 

What Is An Embedded Experience?

Embedded experiences allow application developers to embed content from their applications inside OpenSocial 2.0 containers.  An embedded experience can be built as an OpenSocial gadget, or it can be a simple web page.  Because both are based on standard web technologies like HTML, CSS, and Javascript, any web developer should be able to create an embedded experience.  Embedded experiences are backed by a simple data model.  Containers and gadgets which support embedded experiences will recognize this data model and can choose to render the embedded experience.

What Type Of Embedded Experience Should I Choose?

As mentioned above, there are two types of embedded experiences: OpenSocial Gadgets and web pages.  Embedded experiences which point to web pages are the simplest to create, especially if the web page you want to use already exists.  Embedded experiences which point to web pages can also be embedded in any container, even if they don't support OpenSocial.  You may choose a URL embedded experience if you just want to show static content that is publicly available on the internet.  Good examples of this would be an embedded experience which tracks a package or an embedded experience which asks a user to fill out a survey.  These types of applications are usually public content which does not require authentication to access.  However you will also be limited on the type experience you can provide with a web page.  You will not be able take advantage of any of the OpenSocial APIs when you are using an URL embedded experience.

Embedded experiences based on gadgets have the ability to be more powerful and take advantage of all of the OpenSocial APIs.  The number one reason you would choose a gadget over a web page is because the user needs to authenticate to view or take action on the content in the embedded experience.  OAuth is part of the OpenSocial specification and it can allow your embedded experience gadget to access secure content once the user has delegated access to the gadget.  Embedded experience gadgets can also use any of the OpenSocial APIs.  Your gadget can access the users friends, send messages to other users, or contribute actions to the surrounding container.

What Is This "Data Model"?

The embedded experiences data model is the way to represent an embedded experience in JSON or XML.  It tells the container everything is needs to know so it can render the embedded experience.  The data model has only a couple of properties.

  • gadget - The URL to the gadget you would like to use as an embedded experience.
  • url - The URL to a text/html page you would like to use as an embedded experience.
  • context - The context is information you would like to use in your gadget.  Note: the context is only used for gadget embedded experiences.
  • previewImage - This is a URL to an image which can be used as a preview of the embedded experience.  This information is not necessary but is a convenience to the container if it doesn't want to render the embedded experience immediately but would instead want to show a preview of the content that could be rendered.

All embedded experiences must include EITHER a gadget OR URL in order to be valid embedded experience.  The data model MAY choose to include both if the application wants to provide both types of embedded experiences.  Services may include any combination of the other properties based on their use cases.  Here are some example data models.

URL Embedded Experience
XML
<embed>
  <url>http://example.com/ee.html</url>
</embed>
JSON
{
  "url": "http://example.com/ee.html"
}
Gadget Embedded Experience
XML
<embed>
  <gadget>http://example.com/EE_Gadget.xml</gadget>
</embed>



JSON
{
  "gadget": "http://example.com/EE_Gadget.xml"
}
Gadget And URL Embedded Experience
XML
<embed>
  <url>http://example.com/ee.html</url>
  <gadget>http://example.com/EE_Gadget.xml</gadget>
</embed>
JSON
{
  "url": "http://example.com/ee.html",
  "gadget":  "http://example.com/EE_Gadget.xml"
}
Gadget Embedded Experience With Context
XML
<embed>
  <gadget>http://example.com/EE_Gadget.xml</gadget>
  <context>
    <title>Foo</title>
    <id>1234</id>
  </context>
</embed>
JSON
{
  "gadget": "http://example.com/EE_Gadget.xml",
  "context": {
    "title": "Foo",
    "id": "1234"
  }
}
Gadget And URL Embedded Experience With Context And Preview Image
XML
<embed>
  <url>http://example.com/ee.html</url>
  <gadget>http://example.com/EE_Gadget.xml</gadget>
  <context>
    <title>Foo</title>
    <id>1234</id>
  </context>
  <previewImage>http://example.com/EE_Preview.png</previewImage>
</embed>
JSON
{
  "url": "http://example.com/ee.html",
  "gadget": "http://example.com/EE_Gadget.xml",
  "context": {
    "title": "Foo",
    "id": "1234"
  }
  "previewImage": "http://example.com/EE_Preview.png"
}

How Do I Get Started Developing My Own Embedded Experience?

Now that you know what an embedded experience is, and we know what makes up an embedded experience, we are ready to get started building one.  In this tutorial we will build a gadget based embedded experience since that is the more complex and interesting type of embedded experience.  If you're interested in building a URL embedded experience it's as simple as building a web page and placing the URL to that page in the data model you want to use.  See the sections entitled "Embedded Our Gadget In An Email" and "Embedding Our Gadget In An Activity In An Activity Stream" on how to embed the data model in emails or activity streams.

For this tutorial we will build an embedded experience gadget which will embed a YouTube video in both emails and activity streams.  The first thing we need to do is decide on what our data model is going to look like.  For this tutorial we will keep it fairly simple.  Our embedded experience gadget will just embed a YouTube video.  Lets say we want to embed a video to an OpenSocial tutorial,  http://www.youtube.com/watch?v=9gW2YVBrNVA.  As most people know you can already embed YouTube videos into any HTML page using this HTML.

<object width=480" height="285">
  <param name="movie" value="http://www.youtube.com/v/9gW2YVBrNVA"></param>
  <param name="allowFullScreen" value="true"></param>
  <param name="allowscriptaccess" value="always"></param>
  <embed src="http://www.youtube.com/v/9gW2YVBrNVA" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="285">
  </embed>
</object>

We could just place this HTML in the gadget use that gadget as our embedded experience.  Let's take a look at what an embedded experience gadget and data model would look like using this HTML.

Gadget XML
<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="YouTube Player" description="YouTube Player Using Embedded Experiences" height="400" width="700"/>
<Content type="html">
  <![CDATA[
    <object width=480" height="285">
  <param name="movie" value="http://www.youtube.com/v/9gW2YVBrNVA"></param>
  <param name="allowFullScreen" value="true"></param>
  <param name="allowscriptaccess" value="always"></param>
  <embed src="http://www.youtube.com/v/9gW2YVBrNVA" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="285">
  </embed>
</object>
  ]]>
</Content>
</Module>
Data Model
{
  "gadget": "http://example.com/YouTube_EE.xml"
}

The problem with this embedded experience is that it is very static.  What if we wanted to create an embedded experience for a different YouTube video?  With our current embedded experience we would have to build a new gadget.  Fortunately there is a way to solve this problem.

Using The Embedded Experiences Context

The context property in the embedded experiences data model can be used to make a more abstract embedded experiences gadget.  If we wanted to show a different video using the gadget above the only thing we would need to change is the video id in the URLs in the param and embed tags.  Based on that, the only data that needs to be abstracted from the gadget above is the YouTube video id.  To do this we can change our data model so the context property includes the video id, that way its not hard coded into the gadget.  Here is what our new data model will look like.

{
  "gadget": "http://example.com/YouTube_EE.xml",
  "context": "9gW2YVBrNVA"
}

Our gadget however needs to be able to get the context information from the data model.  This is where we can take advantage of the embedded experiences feature in the OpenSocial specification.  Here is the new gadget XML.

<?xml version="1.0" encoding="UTF-8"?>
<Module>
  <ModulePrefs title="YouTube Player" description="YouTube Player Using Embedded Experiences" height="400" width="700">
    <Require feature="embedded-experiences"></Require>
    <Require feature="dynamic-height"></Require>
  </ModulePrefs>
  <Content type="html" view="embedded,default">
   <![CDATA[
     <script type="text/javascript">
     function showPlayer(context){
       document.getElementById("player").innerHTML = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/' + context + '?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + context + '?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';
       gadgets.window.adjustHeight();
     }

     function initData() {
       opensocial.data.getDataContext().registerListener('org.opensocial.ee.context', function(key){
           showPlayer(opensocial.data.getDataContext().getDataSet(key));
         });
     }

     gadgets.util.registerOnLoadHandler(initData);
   </script>

   <div id="player">
   </div>
  ]]>
  </Content>
</Module>​

The first difference you will notice in our new gadget XML is that we have included two features in the ModulePrefs section.  The dynamic-height feature is used to adjust the height of the gadget once the video has been placed in the gadget.  The more important feature is the embedded-experiences feature.  This is the feature which will get the context information and make it available to the gadget.  The context information from the data model is placed in the data context object, under the key org.opensocial.ee.context.  You can access information from the data context object by using the data context APIs to get the data set for the embedded experiences key.

Notice in the content section of the gadget we have also removed all the HTML from our previous embedded experience gadget and now just have a script tag and a div tag.  If we take a look at the contents of the script tag you will see two functions and a call to register an on load handler to kick everything off.  The on load handler we register, initData, will be called once the gadget has been loaded.  When initData is called we register a listener for the key org.opensocial.ee.context on the data context object.  When the data set attached to the key org.opensocial.ee.context has changed our listener function will be called and can retrieve the data set for that key.  In the embedded experiences case the data set is the context information from the data model.  The data set, or context information, can be retrieved by calling opensocial.data.getDataContext().getDataSet('org.opensocial.ee.context').  In our example embedded experience gadget we call showPlayer with the context information, which in our data model is the YouTube video id.  The showPlayer function sets the inner HTML of the div element with the id player to the HTML to show the YouTube video.

The above embedded experience gadget can now be used to provide an embedded experience for any YouTube video.  All we have to do as the application is change the context information in our data model.  This is much simpler than having to have a separate gadget for every YouTube video.

Embedding Our Gadget In An Email

Now that we know what our data model is going to look like and we have written our gadget we are ready to start providing embedded experiences.  One of the most interesting use cases is to provide a richer experience in emails.  The majority of services today send static HTML emails based on a standard called MIME.  This is a step above plain text emails but HTML has its own limitations as well.  Embedded experiences has taken advantage of the MIME email spec to allow applications to send embedded experiences in MIME emails.  If an application is already sending MIME emails, the changes will be minimal and it will not effect users who are using mail clients that don't support embedded experiences.  

You can add an embedded experience to a MIME email by simply adding an additional MIME part to the MIME email.  Lets say our application sends this MIME email today.

From: notifications@example.com
To: johndoe@example.com
Subject: Check Out This YouTube Video
MIME-Version: 1.0
Content-Type: multipart/alternative;
        boundary="XXXXboundary text"

--XXXXboundary text
Content-Type: text/plain

Check out this YouTube video.
http://www.youtube.com/watch?v=9gW2YVBrNVA

--XXXXboundary text
Content-Type: text/html

Check out this <a href="http://www.youtube.com/watch?v=9gW2YVBrNVA">YouTube video</a>.

This is a pretty standard multipart/alternative MIME email.  It has two parts, a text/plain and a text/html part.  Like I said above, to make this an embedded experience email we just have to add another part.

Embedded Experience MIME Email With JSON Data Model
From: notifications@example.com
To: johndoe@example.com
Subject: Check Out This YouTube Video
MIME-Version: 1.0
Content-Type: multipart/alternative;
        boundary="XXXXboundary text"

--XXXXboundary text
Content-Type: text/plain

Check out this YouTube video.
http://www.youtube.com/watch?v=9gW2YVBrNVA

--XXXXboundary text
Content-Type: text/html

Check out this <a href="http://www.youtube.com/watch?v=9gW2YVBrNVA">YouTube video</a>.

--XXXXboundary text
Content-Type: application/embed+json
{
  "gadget" : "http://example.com/YouTube_EE.xml",
  "context" : "9gW2YVBrNVA"

}
Embedded Experience MIME Email With XML Data Model
From: notifications@example.com
To: johndoe@example.com
Subject: Check Out This YouTube Video
MIME-Version: 1.0
Content-Type: multipart/alternative;
        boundary="XXXXboundary text"

--XXXXboundary text
Content-Type: text/plain

Check out this YouTube video.
http://www.youtube.com/watch?v=9gW2YVBrNVA

--XXXXboundary text
Content-Type: text/html

Check out this <a href="http://www.youtube.com/watch?v=9gW2YVBrNVA">YouTube video</a>.

--XXXXboundary text
Content-Type: application/embed+xml
<embed>
  <gadget>http://example.com/YouTube_EE.xml</gadget>
  <context>9gW2YVBrNVA</context>
</embed>

In our new email you can see we have added another part with the content type of application/embed+json.  This is a MIME type the OpenSocial foundation has registered with IANA to represent embedded experiences.  In addition to the JSON MIME type the OpenSocial foundation has also registered applicaiton/embed+xml for the XML data model.  The content of the MIME part is just our embedded experiences data model.  When email clients which support embedded experiences receive this email, they will be looking for a MIME part with the type application/embed+json or application/embed+xml, and if they find it they can choose to render the embedded experience.  If a client which does not support embedded experiences receives this email they will choose to render whatever other part they support, most likely the text/html part.

Embedding Our Gadget In An Activity In An Activity Stream

OpenSocial 2.0 supports the ActivityStreams 1.0 specification.  OpenSocial has also defined a namespace extension to the ActivityStreams spec which contains OpenSocial specific concepts.  The embedded experiences data model can be placed inside the OpenSocial extension.  Say we have this activity entry.

{
  "postedTime": "2011-02-10T15:04:55Z",
  "actor": {
    "url": "http://example.com/john",
    "objectType" : "person",
    "id": "tag:example.com,2011:john",
    "image": {
      "url": "http://example.com/john/image",
      "width": 250,
      "height": 250
    },
    "displayName": "John Doe"
  }
  "verb": "post",
  "object" : {
    "url": "http://www.youtube.com/v/9gW2YVBrNVA",
    "id": "tag:youtube.com,2011:9gW2YVBrNVA"
  },
  "target" : {
    "url": "http://example.org/favorites/",
    "objectType": "favoriteslist",
    "id": "tag:youtube.com,2011:9gW2YVBrNVA",
    "displayName": "John's Favorites"
  }
}

To add our embedded experience to this activity entry we need to add the OpenSocial namespace extension and then add our embedded experience data model inside of it.

{
  "postedTime": "2011-02-10T15:04:55Z",
  "actor": {
    "url": "http://example.com/john",
    "objectType" : "person",
    "id": "tag:example.com,2011:john",
    "image": {
      "url": "http://example.com/john/image",
      "width": 250,
      "height": 250
    },
    "displayName": "John Doe"
  }
  "verb": "post",
  "object" : {
    "url": "http://www.youtube.com/v/9gW2YVBrNVA",
    "id": "tag:youtube.com,2011:9gW2YVBrNVA"
  },
  "target" : {
    "url": "http://example.org/favorites/",
    "objectType": "favoriteslist",
    "id": "tag:youtube.com,2011:9gW2YVBrNVA",
    "displayName": "John's Favorites"
  }
  "openSocial" : {
    "embed" : {
      "gadget": "http://example.com/YouTube_EE.xml",
      "context": "9gW2YVBrNVA"
    }
  }
}

We can now post this activity to any activity stream and if the container rendering the activity stream supports embedded experiences it can choose to display the embedded experience in the activity stream as well.

Resources

  1. Embedded Experiences - http://opensocial-resources.googlecode.com/svn/spec/2.0/Core-Gadget.xml#Embedded-Experiences
  2. OAuth In OpenSocial - http://opensocial-resources.googlecode.com/svn/spec/2.0/Core-Gadget.xml#OAuth
  3. Data Context - http://opensocial-resources.googlecode.com/svn/spec/2.0/Core-Gadget.xml#DataContext_Client
  4. OAuth Spec - http://oauth.net/
  5. Activity Streams Spec - http://activitystrea.ms/
  6. MIME RFC - http://www.rfc-editor.org/rfc/rfc2046.txt