MessengerDev

Developing with the Windows Live Messenger Library

Using Custom Presence with the Windows Live Messenger Library

Posted by JasonKelly Chat with Me!

The Windows Live Messenger Library gives you a way to add custom presence properties to the signed-in user.  There are a bunch of ways you can use this.

The most basic use for custom presence is to let your WLML application know which of the user's contacts are signed into the same application. By knowing that, you can show an icon or light up extra features when two people are chatting in your application.

Another way to use it is to add some personal information that goes above and beyond the normal Personal Message in Messenger. For example, if you're adding Messenger to a music site, you can add information on what music is now playing.

I've implemented a simple example here. This is the same as the

Messenger Hello World application, but it uses custom presence to show a * when one of the users in your contact list is also signed into the example application.

How to implement Custom Presence

To use custom presence, you need to define two new objects in your code. The first is the presence extension object that will be added to the user's presence:

    var EXTENSION_NAME = ‘ExtNm’ // 6 chars or less
    function MyPresenceExtension(name, content) {   
       this.name = name;   
       this.content = content; } 
    MyPresenceExtension.prototype.get_name = function() {   
        return this.name; 
    }
    MyPresenceExtension.prototype.get_content = function() {   
        return this.content; 
    }

The presence extension object must support the get_name() function. The name has to be 6 or fewer alphanumeric characters. This is a simple example of a presence extension with just a name/value pair.

Then, create a presence extension factory. This is an object that knows how to serialize and deserialize your presence extension to and from a string.

	MyPresenceFactory = function () {} 
	MyPresenceFactory.prototype.serialize = function(prop) { 
	return prop.get_content(); 
	} 
	MyPresenceFactory.prototype.deserialize = function(name, content) { 
	return new MyPresenceExtension(name, content); 
	}

After these objects are defined, you can hook it up. After sign in, add an instance of your custom presence factory to the user object, and then add the property itself to your user (here the content is just set to '*'):


	/* Called by the Messenger Library when sign-in is complete. */ 
    function signInCompleted(sender, e) {
        if (e.get_resultCode() === Microsoft.Live.Messenger.SignInResultCode.success) { 
        _user.set_presenceFactory(new MyPresenceFactory());
        addCustomPresence();
      [...]	

	/* Add a custom presence extension to the signed-in user */
	function addCustomPresence() { 
    endpointCollection = _user.get_endpoints(); 
        endpointPresence = endpointCollection.get_item(0).get_presence();
        extensionCollection = endpointPresence.get_extensions();
        extensionCollection.add(new MyPresenceExtension(EXTENSION_NAME, '*'));
    }
 

Now all that's left is to look for the property on users in the contact list.               

    // for each endpoint of this contact's presence, check for presence extension
    // the address comes from Contact.get_currentAddress()
    function getCustomPresenceContent(address) {
        endpointEnum = address.get_endpoints().getEnumerator();
        while (endpointEnum.moveNext()) {
            endpoint = endpointEnum.get_current();
            extensionEnum = endpoint.get_presence().get_extensions().getEnumerator();
            while (extensionEnum.moveNext()) {
                extension = extensionEnum.get_current();
                if (extension.get_name() == EXTENSION_NAME) {
                    return extension.get_content();
                }
            }
        }
        return '';
    }
Hope this helps get you started with custom presence. Questions? Post in the Messenger API Forums on MSDN. I'll answer any of them that I can.
Posted on: 3/28/2008 at 4:52 AM
Actions: E-mail | Digg It! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Sunday, September 07, 2008 6:00 AM