Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Subscribing to Presence

An endpoint can subscribe to its own presence information (self presence) or to the presence information published by other presentities. Although it might seem odd for an endpoint to subscribe to self presence, a typical scenario is that an endpoint owner can own several endpoints, none of which has presence information about the other endpoints. By subscribing to self presence, a given endpoint can receive current information about the other endpoints from the presence repository on the Office Communications Server computer.

Subscribing to Self Presence

A UserEndpoint subscribes to its self presence to get category information about its own containers, contacts, groups, and subscribers from the server, and to synchronize the states of multiple UserEndpoint instances a user may have so that they all reflect the same user state. It is also important for a UserEndpoint instance to subscribe to self presence because it is only on the self subscription containers and categories that bootstrapping occurs. The reason for this is that the platform creates an initial set of containers and members of these containers based on rules in the container manifest (such as adding the sameEnterprise flag to container 200 or publicCloud to 100). The platform also creates an initial set of categories in certain other containers based on publication manifest rules (such as an offline aggregatePresence category added to 32000 [blocked container]). Bootstrapping happens only for UserEndpoint instances, not for ApplicationEndpoint instances.

// Register handlers for events that will be raised.
_userEndpoint.LocalOwnerPresence.CategoryNotificationReceived += LocalOwnerPresence_CategoryNotificationReceived;
_userEndpoint.LocalOwnerPresence.ContainerNotificationReceived += LocalOwnerPresence_ContainerNotificationReceived;
_userEndpoint.LocalOwnerPresence.SubscriberNotificationReceived += LocalOwnerPresence_SubscriberNotificationReceived;
_userEndpoint.LocalOwnerPresence.DelegateNotificationReceived += LocalOwnerPresence_DelegateNotificationReceived;

_userEndpoint.LocalOwnerPresence.BeginSubscribe(PresenceSubscribeCompleted, _userEndpoint.LocalOwnerPresence);
…

private void PresenceSubscribeCompleted(IAsyncResult result)
{
  try
  {
    LocalOwnerPresence lop = result.AsyncState as LocalOwnerPresence;
    lop.EndSubscribe(result);
  }
   catch (PublishSubscribeException pse)
  {
     // Write exception handling code here.
  }
  catch (RealTimeException rte)
  {
     // Write exception handling code here.
  }

}
// Event handler to parse receiving category notifications. 
void LocalOwnerPresence_CategoryNotificationReceived(object sender, CategoryNotificationEventArgs e)
{
  foreach (PresenceCategoryWithMetadata category in e.CategoryList)
  {
    …
  }
}

// Event handler to parse receiving container notifications. 
void LocalOwnerPresence_ContainerNotificationReceived(object sender, ContainerNotificationEventArgs e)
{
  foreach (ContainerMembership membership in e.ContainerList)
  {
    Console.Writeline("Notification from " + membership.ContainerId +
      "that contains " + membership.AllowedSubscribers.Count.ToString() +
      "subscribers, " + membership.AllowedSipDomains.Count.ToString()+ "domains");
    Console.Writeline("SameEnterprise flag been set " + 
      membership.AllowedSourceNetworks && SourceNetwork.SameEnterprise;
      …
  }  
…
}

// Event handler to parse receiving subscriber notifications. 
void LocalOwnerPresence_SubscriberNotificationReceived(object sender, SubscriberNotificationEventArgs e)
{
  foreach (Subscriber subscriber in e.WatcherList)
  {
    if (!subscriber.IsAcknowledged)
    {
      _endpoint.LocalOwnerPresence.BeginAcknowledgeSubscriber(subscriber.Id,
AcknowledgeSubscriberCallback, _endpoint.LocalOwnerPresence);
    }
  }
}

// Event handler to parse receiving delegatee notifications. 
void LocalOwnerPresence_DelegateNotificationReceived(object sender, DelegateNotificationEventArgs e)
{
  foreach (Delegatee delegate in e.DelegateList)
  {
    …
  }
}

Subscribing to the Presence of Other Users

In addition to an endpoint subscribing to self presence, and endpoint can subscribe to the presence of other users, using its RemotePresence property.

The RemotePresence property on an endpoint provides the following capabilities.

  • Polled/one-time query—A subscribing endpoint can use the BeginPresenceQuery() method to make a one-time query. With this type of presence subscription, a watcher receives the presence information and receives no more NOTIFY messages. An endpoint using this type of subscription type must perform another query to get updated presence information from the publisher.

  • Persistent subscription—A subscribing endpoint can use the AddTargets method to make a persistent subscription. For this type of subscription, the subscription remains active until the endpoint is disabled or the endpoint removes the subscription. Whenever the publisher changes information, the watcher receives a NOTIFY message.

The following procedure shows how to subscribe to the presence information of remote presentities.

To subscribe to the presence information of a remote presentity

  1. Set a local variable with the value of the RemotePresence property on the endpoint.

  2. Register for the PresenceNotificationReceived event.

  3. Create a RemotePresentitySubscriptionTarget instance for each person whose presence information you wish to subscribe. Null context data ensures that the target contact is not notified of the subscription. Non null context data allows the target to be informed about the subscription. We recommend passing String.Empty for better OC interoperation.

  4. Create a List that contains the subscription targets.

  5. Subscribe to presence information using BeginAddTargets() and EndAddTargets(). These two methods conform to the .NET asynchronous programming pattern.

// Register for PresenceNotificationReceived event.
_userEndpoint.RemotePresence.PresenceNotificationReceived += new RemotePresence_PresenceNotificationReceived;

RemotePresentitySubscriptionTarget target1 = new RemotePresentitySubscriptionTarget("sip:alice@contoso.com", null);
RemotePresentitySubscriptionTarget target2 = new RemotePresentitySubscriptionTarget("sip:brad@contoso.com", null);
RemotePresentitySubscriptionTarget target3 = new RemotePresentitySubscriptionTarget("sip:carmen@contoso.com", null);

List<RemotePresentitySubscriptionTarget> targets = new List<RemotePresentitySubscriptionTarget>();
targets.Add(target1);
targets.Add(target2);
targets.Add(target3);

   
_userEndpoint.RemotePresence.BeginAddTargets(targets, SubscribeToRemoteUsersCompleted, _userEndpoint.RemotePresence));

private void SubscribeToRemoteUsersCompleted(IAsyncResult result)
{
  try
  {
    RemotePresence remotePresence = result.AsyncState as RemotePresence;
    remotePresence.EndAddTargets(result);
  }
  catch (PublishSubscribeException pse)
  {
    // Write exception handling code here.
  }
  catch (RealTimeException rte)
  {
    // Write exception handling code here.
  }

}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.