Export (0) Print
Expand All

Sending and Receiving Application Presence (JavaScript Library)

Ff748155.note(en-us,MSDN.10).gifNote:
Current information about Live Connect is now available in the Windows Live Developer Center. The information in the following sections is provided for legacy purposes only.

Presence extensions are custom types that provide a means to set custom presence properties for users of your Web-based applications. You can use presence extensions to enable users to publish presence information for other users to see. For example, a music web site with an integrated Windows Live Messenger experience might publish "Now Playing" information as part of the users' Messenger presence. Custom presence properties can perform a range of tasks, such as detecting when a particular pair of users is signed in.

To implement custom presence, decide if your application will use a default factory and presence extension class, or whether you will implement your own. The default presence extension factory is simpler to use, but only supports serializing and deserializing JavaScript Object Notation (JSON) objects.

To set a presence extension using the default factory and extension class
  1. Create a new instance of the SimplePresenceExtension class with the specified name and presence value.

    var extension = new Microsoft.Live.Messenger.Presence.SimplePresenceExtension("pExt1", { param1: "value1" });
    
  2. Set the user presence to the SimplePresenceExtension instance that you just defined.

    Microsoft.Live.App.get_messengerContext().get_user().get_localEndpoint().get_presence().get_extensions().add(extension);
    

If your application needs more specialized logic for its presence extensions, then it must implement its own extension of the PresenceExtension and PresenceExtensionFactory classes.

  • Create a presence extension object. This object must support the get_name() method, and can be extended to contain other methods and data.
  • Create a PresenceExtensionFactory object with two methods:
    The serialize method outputs a string representation of the presence extension object.
    The deserialize method recreates the presence extension from the string representation.
  • Set your PresenceExtensionFactory as the active presence factory.
  • Add the presence extension to the users' presence.
To create a presence extension object
  1. In your JavaScript file, declare a variable that contains the extension name.

    This value must be unique within the application, cannot exceed 6 characters, and cannot start with a number.

    var extNm = 'ExtNm1';
    
  2. Create the necessary code for the functionality of the extension.

    In this example, the extension stores a name-value pair. The function is used as a constructor, and the other functions in the class are defined by using the prototype property.

    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;
    }
    
To create a PresenceExtensionFactory object
  1. Declare the PresenceExtensionFactory object.

    This object serializes and deserializes your custom presence extension.

    MyPresenceFactory = function () {}
    
  2. Create the serialize and deserialize functions.

    MyPresenceFactory.prototype.serialize = function(prop) {
        return prop.get_content();
    }
    MyPresenceFactory.prototype.deserialize = function(name, content) {
        return new MyPresenceExtension(name, content);
    }
    
To set the PresenceExtensionFactory as the active presence factory
  • Add the presence factory to the user object by calling set_presenceFactory, and then creating a new instance of MyPresenceFactory.

    Microsoft.Live.App.get_messengerContext().get_user().set_presenceFactory(new MyPresenceFactory());
    
To add the presence extension to users' presence
  1. Add the custom presence extension for the signed-in user.

    function addCustomPresence() 
    {
        endpointCollection = Microsoft.Live.App.get_messengerContext().get_user().get_endpoints();
        endpointPresence = endpointCollection.get_item(0).get_presence();
        extensionCollection = endpointPresence.get_extensions();
        extensionCollection.add(new MyPresenceExtension(extensionName, '*'));
    }
    
  2. Add code to check for the presence extension in each endpoint of this contact's presence.

    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() == extensionName) 
                {
                    return extension.get_content();
                }
            }
        }
        return '';
    }
    

You can use JSON to send multiple data values in one string. In the following code example, JSON is used to create a set of name-value pairs to represent latitude and longitude coordinates. For more information, see the JSON website.

// Create the JSON string.
var latLonStr = "{'lat':'47.7575', 'lon':'-122.24278'}";

// Use the eval function to convert the string into an object.
// Typically, this is used on the receiving side.
var latLonObj = eval('(' + latLonStr + ')');

// Test it out by using an alert.
alert('latitude: ' + latLonObj.lat + ', longitude: ' + latLonObj.lon);

The following example demonstrates the process to implement the example name-value custom presence extension.

// **** CUSTOM PRESENCE ****
MyPresenceFactory = function () {}
MyPresenceFactory.prototype.serialize = function(prop) {
    return prop.get_content();
}

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

var extensionName = 'MLSDK';
   
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;
}

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

// For each endpoint of this contact's presence, check for presence extension.
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() == extensionName)
            {
                return extension.get_content();
            }
        }
    }
    return '';
}
// **** END CUSTOM PRESENCE ****
Show:
© 2014 Microsoft