Live Spaces: The challenges facing gadget developers

My name is Donavon West, a Live.com and Sidebar gadget developer at LiveGadgets.net and a Microsoft MVP for Windows Live Development. This is my first blog article here at LiveSide. Many of you have written gadgets for Live.com (what Microsoft is now referring to as "web gadgets").  The introduction of gadgets on Windows Live Spaces introduces many new challenges for developers. Here are a few: 

Author vs. Visitor modes

With traditional Live.com gadgets, the person that placed a gadget on the page was the only person to view it. Windows Live Spaces a new paradigm. Now anyone with access to the Internet can view your gadget. But, you don't want to give everyone the right to change the gadget's settings. Because of this, gadgets now operate in one of two modes: author and visitor.

Let's examine the two modes using the iTunes gadget that I recently wrote. It displays album art, song title and artist from the iTunes most popular songs RSS feed. It also allows the user (we'll get to the definition of "user" in a moment) to select from a list of genres to display as well as the color of the virtual "iPod".

When you place the iTunes gadget on a page in Live.com, the user can adjust the various settings. This is what is called "author mode". In author mode, controls are exposed by the gadget to alter it's behavior. For example the user/author could select the genre Hip Hop/Rap. With Live.com there is only one mode: author.

With Windows Live Spaces, the author is the person who "owns" the site (i.e. yoursite.Spaces.Live.com). The "visitor" would be one of the visitors to the site. In many cases you do not want viewers to change certain aspects of the gadget. In the previous example, if you place the iTunes gadget on your space and set it up to display Hip Hop/Rap (because that's the kind of music you like), you may not want your users to alter this. Therefore, in visitor mode, setting controls are hidden (i.e. there is no "Change Genre" button).

setPreference doesn't work!

Normally, when you want to save some user setting (again, like the genre mentioned above) in author mode, you would call p_args.module.setPreference() with the name/value pair that you would like to persist. This works fine when your gadget runs on Live.com. If you try and do the same thing in Live Spaces the underlying HTTP call returns a response code of 500 (SERVER ERROR) and your settings are lost.

What can you do about this? About the only thing you can do (short of praying to the Spaces God, or Gods if you are a BSG fan) is to write and host your own data store. But before you do, consider this; your gadget may end up of thousands of spaces around the world. This means thousands of database records to store the gadget's settings and potentially millions of HTTP hits on your server. If your hosting company charges by the megabyte, this could amount to a costly gadget.

So all you have to do is write a web service and call it with the user data and some unique ID. On Live.com you would do a module.GetId(), but when you try this on Live Spaces, you get some long string something like this:

GadgetGallery:http://somedomain.com/gadget/gadget.xml

Not much good as this is the same ID that you will get on each of the thousands of Live Spaces on which your gadget resides. No, you need something to uniquely identify the Live Space that you gadget is installed on. Fortunately, I know the secret and equally as fortunate for you, I'm going to tell you what the secret is. 

But first, here's a hint. On your Live Space, right-click somewhere on the background of your gadget's iframe and select properties. Take a look at the URL. Do you see anything that might be unique to the particular space you gadget is running on? Yes, you do. There is a "&host=foo.spaces.live.com" in the URL.

Sweet, you say. But how does that help me? Well I thought you might ask that. All you have to do is parse the URL and return all query string and hash (the stuff following the "#") parameters. When you are done, the value for host will be your unique ID. (Note: this ID is unique to the apace and not to an instance on the space; that is to say that you can only have one copy of your gadget on any given Live Space). Oh, I'm not going to write the parsing code for you. You'll have to figure that out for yourself. :)

Transparent background

With Live.com gadget, programmers just got used to setting the backgroundColor of their DIV class to white/#ffffff or leaving it blank to inherit the background color of the body. The body of a non-certified gadget is the controlled by the HTML page that loaded in the iframe. You gadget runs inside of a DIV on this HTML page. As any piece of code running on a web page, you have full control of the document (and thus the body element) of the page.

To successfully have a transparent background in an iframe, you need 2 things. 1) the parent iframe element must set attribute allowTransparency="true". As luck would have it, the Live Spaces people have been gracious enough to oblige.

The other part of the equation is to set the backgroundColor of the body element of the HTML page within the iframe to transparent. Here is the code to do just that:

if (window.parent != window.self) {
    document.body.style.backgroundColor = "transparent";
}

If you look at the example shown above, the gadget on the left has the code above applied. The gadget on the right has not. Note that the 2 gadgets also have different background iPod images (one black and one white). This has nothing to do with the backgroundColor code.

 

p_args are different

Arrrrrrg! When a gadget is instantiated, it is passed 3 parameters: p_el, p_args and p_namespace (p_namespace if currently unused). p_el is a pointer to the DIV element where your gadget is bound to or "lives". p_args is an object that contains many useful properties. Here is what p_args looks like in VS2005 with the iTunes gadget running on Live.com compared to Live Spaces:

//Live.com
feed: {…}
feedUrl: "http://localhost/gadgets/itunes2/gadget.xml"
xml: {…}
defaults: {…}
module: {…}
loc: "us"
mkt: "en-us"
lang: "en"
fullMode: false
paramString: "{\"uri\":\"http://localhost/gadgets/itunes2/gadget.xml\",\"listIcon\":null}"
mode: "author"
id: "a80d2672-0058-42d1-b952-1e718b6565bd"
gadgetHost: {…}
uri: "http://localhost/gadgets/itunes2/gadget.xml"
listIcon: null
onDashboard: true
xmlSources: {…}

//Live Spaces
xml: {…}
module: {…}
onDashboard: false
ownermkt: "en-us"
alias: undefined
xmlSources: {…}

Wow, that's a big difference. I won't go into the meaning of them all, but some notable standouts include: defaults and feed. Of course it would be nice if id were set to the Live Spaces host name so we wouldn't have to go through all that trouble that we did above. Oh, and the "M" in ownermkt should be capitalized. ;)

Test Platform
gadget

To help to keep tabs on what p_args are supported on Live.com and Live Spaces, I've written a tool called TestPlatform. The manifest URL is:  http://test.livegadgets.net/gadgets/TestPlatform/gadget.xml. It displays 2 of the most important aspects of the gadget framework, p_args and p_args.module.

 

Gadget Platform Test Page

If you want to see how your web gadget will look on Live Spaces (both author and view modes) and on Live.com, here is a link you should know about. http://test.livegadgets.net/gadgets/TestPlatform/testpages.htm

 

As Live Spaces matures and the line between gadget platforms starts to come together as one codebase, many of these issues will go away. Until then, I hope these tips help you develop better gadgets that play well on Live.com and Live Spaces.

donavon

Comments

  • http://khristopherr.spaces.live.com Khristopher

    i understand the problems with the gadgets that devellopers might have, but the gadgets being made are absolutely crap and so far i’ve used none that anyone besides MS has made.
    for example the itunes one you have made. why would i want that? i have itunes on my PC and can access all of that info. now if you made it useful like to show the songs i’ve been listening to or something that might be a little more interesting. if i had the know-how to make my own gadget i’d try myself but i am not a developer i’m a user so.

  • http://theoceanviewnet.spaces.live.com/blog/ TheViewMaster

    Aloha! Donavon! ;-)

    Microsoft Appears To Have Opted For “Gimmicks” Over “User Content” With Regard To Windows Live Spaces!

    These “Gadgets” Appear To Be The Cause of EXTREMELY S-L-O-W Loading/FREEZING of Windows Live Spaces, Rendering Windows Live Spaces Practically Useless & The Source of MUCH User Discontent!

    Therefore, I Believe MSN Groups: http://groups.msn.com/ Is a Better Venue For Showcasing These “Gadgets” Than, Windows Live Spaces!

    Mahalo! …4 Your HARD Work!

    Aloha!

    ;-)

  • http://www.livegadgets.net/#blog donavon

    punky,

    It sounds like you wouldn’t want the iTunes gadget, but’s it’s been downloaded nearly 50,000 times on the Microsoft Gallery http://gallery.microsoft.com/liveItemDetail.aspx?li=415b0cfd-9bf2-4287-811f-3f59d75b0c30&l=1 so SOMEONE out there must see a use for it.

    But seriously, it’s not a very practical gadget. As I’m tracking it, it’s appears as though it appeals mainly to 14-16 y/o girls from China so they can have a slide show featuring album art of the genre of music that they like so their friends can see it when they visit their Live Space. Not my cup-o-tea, but oh well.

    And while I agree that the majority of the gadgets on the gallery are crap (there has been a rash of gadget “spam” as of late), there are some very well written gadgets out there. Personally I’ve written around 20 gadgets and maybe only 1 or 2 have been crap. ;) I even won an Xbox for one of my gadgets http://www.livegadgets.net/#blog_3acce59e-c7d1-40a2-8047-e9c380fa65e5 and a Creative Zen media player for another http://www.livegadgets.net/#blog_b225adea-8059-48d7-9e71-bcee5e9a0ef9.

    donavon

  • toddos

    How do you go about adding a gadget to your Spaces page manually for testing purposes? So far that’s eluded me, though I will admit that I haven’t spent much time really thinking about Spaces gadgets.

    Your article makes me want to implement a simple storage mechanism so I can start doing some more interesting Spaces-related gadgets.

  • http://www.livegadgets.net/#blog donavon

    Todd,

    To add a gadget to your space, use this (from a post on thespacecraft, the Spaces team blog):

    http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=URLEncodedPathToYourHostedGadgetManifestURL

    The permalink to the full article:
    http://thespacecraft.spaces.live.com/blog/cns!8AA773FE0A12B9E3!23013.entry

  • toddos

    Thanks to this article, I finally went ahead and implemented my own preference storage mechanism. Soon, my “Xbox 360 Favorite Games” gadget (http://gallery.live.com/liveItemDetail.aspx?li=33e9aed1-bd31-43b6-aaff-b8d9a1f39719&l=1) will have proper Spaces support (just as soon as it makes it through Gallery approval — check back tomorrow morning). In the process, I’ve got a couple more questions that perhaps you or someone from Spaces can answer:

    1. Is there any way to grab the style from a Space? Gadgets tend to assume a dark-on-light theme, but there are plenty of Space themes that are light-on-dark. By blindly setting the gadget background to transparent, you may end up with unreadable text (black text on a black or dark background).
    2. Is there any way to allow a gadget to use more horizontal space? Gadgets squeeze fine if you put them into a narrower portion of your layout, but they don’t stretch out all the way if you put them in a wider part.
    3. Do you have any recommendation on how best to determine if a gadget is running on Spaces? The best I found was to check the window.location for the “host” parameter, or match the specific ‘”GadgetGallery:” + p_args.module.resolveUrl(“gadget.xml”)’ string from p_args.module.getId(). Neither seems particularly robust.

  • http://spacesplatform.spaces.live.com Greg Phipps

    Thanks for article Donavon. I have posted a response at http://spacesplatform.spaces.live.com/blog/cns!AEA03ADE93DDFB95!139.entry

  • http://spacesplatform.spaces.live.com Greg Phipps

    Notably for the gadget developer community we have enabled support for Get/Set Preferences with this release. See the post at http://spacesplatform.spaces.live.com/blog/cns!AEA03ADE93DDFB95!148.entry

  • http://www.livegadgets.net/#blog donavon

    Thanks Greg. This is great news for gadget developers. I expect to see more quality gadgets available for Spaces in the near future.

  • http://danielemi.spaces.live.com daniele bonini

    I’m sorry, but about the transparency tips I found here:

    http://www.liveside.net/blogs/developer/archive/2006/10/03/Live-Spaces_3A00_-The-challenges-facing-gadget-developers.aspx

    I can say u it doesn’t function at all. I have already submited three or four new versions of differents gadget implemented as u stated here but I didn’t get any result. Just a white background. As the the theme of my space has a white background I could guess that the problem is from the source of the frame, I’m unable to override the *default setting*.
    http://gallery.live.com/author.aspx?a=46cedced-1b43-44ec-b789-6765a2f11092

    Thank u,

    Daniele

  • http://danielemi.spaces.live.com daniele bonini

    To get u with more problems :-),
    if you can tip the right people from your side,

    the boxes containing the gadgets, can I call them webparts?, have a strange behaviour on the rendering of their size, particularly the height. I guess it is a css problem. In any way, when u load your space some *boxes* are of the right height, some are not at all. The result is usually a space with many blank spaces and the things in disorder. Otherwise u have to reload your space many times until the browser seems to eat it well.

    Hopes this helps and u can find a fast solution.

    Grazie

  • http://danielemi.spaces.live.com daniele bonini

    an other interesting parameter in the gadget url is “A” like “..aspx?a=true” that showes you if u are in edit mode of your space (“True”) or not (“False”)

    ciao :-P

  • http://danielemi.spaces.live.com daniele bonini

    About the ccs problems, the problems rendering the height of the gadgets in Live Spaces.
    I have noticed that if I right click the gadget and I refresh the start.com service which starts the gadget, the gadget is rendered always in the right way. So probably it is a problem of the page loading event or of the start.com service processing all the requests.

    ciao