How to: Monitor Presence

You can monitor the presence of a current Windows Live Messenger user and the user's contacts. Monitoring presence is useful to track of given user's presence and 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 the real-time, updated Live Messenger profile and presence information to a user. The Profile Control shows all of this information in a single view.

The Contact List Control shows a familiar Windows Live Messenger contact list. You do not need to manage the presence information; the UI Controls handle management.

To use the Windows Live Messenger Library to perform these tasks, 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 tasks when the event is fired, for example, changing the text that indicates a user's current status. You 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 the JavaScript file, call propertyChanged with the event handler function, 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.");
    }
Monitoring Contact Collections

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. Subscribe to the CollectionChanged event for both collections.

To subscribe to the CollectionChanged event

  1. In the JavaScript file, call the add_collectionChanged accessor and pass a delegate for the onlineContacts_CollectionChanged event handler function.

    _user.get_onlineContacts().add_collectionChanged(onlineContacts_CollectionChanged);
  2. Call the add_collectionChanged accessor again and pass 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.

    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);
        }
    }
Example

The following code example demonstrates iterating 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.

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);
}
See Also

Concepts

Windows Live Messenger Library Tasks

Page view tracker