Click to Rate and Give Feedback

  Switch on low bandwidth view
How to: Monitor Presence

You can monitor the presence of a current Windows Live™ Messenger user and all of the user's contacts. Monitoring presence is useful for keeping track of the presence for a given user, as well as tracking other properties, such as a display picture or display message text.

Monitoring presence is easy with the Windows Live UI Controls. By using the Display Name Control, Display Picture Control, Presence Status Control, and Personal Message Control, a site can show a user the real-time updated Live Messenger profile and presence information. The Profile Control shows all of this information in a single view.

By using the Contact List Control, a site can show a contact list in the manner with which Windows Live users are familiar. You do not need to manage the presence information; the UI Controls handle such management.

If you would like to use the Windows Live Messenger Library to perform these tasks instead, you need to monitor presence. Monitoring presence involves subscribing to the Presence.PropertyChanged event for a user, and defining an event handler function to perform the desired tasks when the event is fired—for example, changing the text indicating a user's current status. You will need to subscribe to the Presence.PropertyChanged event individually for each user you want to monitor.

To subscribe to the Presence.PropertyChanged event
  1. In your JavaScript file, call propertyChanged with an event handler function (in this case, user_Presence_PropertyChanged).

    _user.get_presence().add_propertyChanged(user_Presence_PropertyChanged);
  2. Create the event handler function.

    function user_Presence_PropertyChanged(sender, e) 
    {
        var propName = e.get_propertyName();
        alert(propName + " has changed.");
    }

The following code example demonstrates iterating through for each contact on the user's contact list and subscribing to the PropertyChanged event for each contact and for the ContactPresence for each contact.

Code

for (var i = 0; i < _user.get_contacts().get_count(); i++)
{
    var c = _user.get_contacts().get_item(i);
    c.add_propertyChanged(contactPropertyChanged);
    c.get_presence().add_propertyChanged(contactPresencePropertyChanged);
}

You can monitor the User.OnlineContacts property and User.OfflineContacts property collections to keep track of which users are online, and which users are offline. To do this, you will need to subscribe to the CollectionChanged event for both collections.

To subscribe to the CollectionChanged event
  1. In your JavaScript file, call the add_collectionChanged accessor, passing a delegate for the onlineContacts_CollectionChanged event handler function.

    _user.get_onlineContacts().add_collectionChanged(onlineContacts_CollectionChanged);
  2. Call the add_collectionChanged accessor once more, this time passing a delegate for the offlineContacts_CollectionChanged event handler function.

    _user.get_offlineContacts().add_collectionChanged(offlineContacts_CollectionChanged);
  3. Create an event handler function for each of the events.

  4. In this example, each event handler iterates through its respective collection and populates an HTML element with the updated data.

    function onlineContacts_CollectionChanged() 
    {
        removeChildrenFromNode("onlineContactList");
        var onlineContactList = document.getElementById("divOnlineContacts");
                   
        for (var i = 0; i < _user.get_onlineContacts().get_count(); i++)
        {
            var c = _user.get_onlineContacts().get_item(i);
            var contactItem = document.createTextNode(c.get_displayName());         
            onlineContactList.appendChild(contactItem);
        }
    }
    
    function offlineContacts_CollectionChanged() 
    {
        removeChildrenFromNode("offlineContactList");
        var offlineContactList = document.getElementById("divOfflineContacts");
                   
        for (var i = 0; i < _user.get_offlineContacts().get_count(); i++)
        {
            var c = _user.get_offlineContacts().get_item(i);
            var contactItem = document.createTextNode(c.get_displayName());         
            offlineContactList.appendChild(contactItem);
        }
    }
    
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker