Enable Presence Functionality

The following code examples demonstrate how to enable presence in RTC. The operations in the Initialize RTC and Register To Receive Events code examples must be performed before using these examples.

Note  These examples do not contain error checking or releases appropriate for real code.

C++ Code Example

IRTCClientPresence *m_pPresence = NULL;
IRTCBuddy *m_pBuddy = NULL;
BSTR bstrBuddyURI = SysAllocString(_T("somebody@example.com"));
BSTR bstrBuddyName = SysAllocString(_T("My Buddy"));
BSTR bstrBuddyData = SysAllocString(_T("Any Data here"));
VARIANT varPresence;

VariantInit(&varPresence);
varPresence.vt = VT_BSTR;
varPresence.bstrVal = SysAllocString(PRESENCE_STORAGE); 

// Get the Presence interface.
hr = m_pIRTCClient->QueryInterface( IID_IRTCClientPresence, 
                               reinterpret_cast<void**> (&m_pPresence));

// If (hr != S_OK), process the error here.

// Automatically add a watcher to the list of allowed watchers.
// There is no need to fire an event.
hr = m_pPresence->put_OfferWatcherMode( 
                  RTCOWM_AUTOMATICALLY_ADD_WATCHER );

// If (hr != S_OK), process the error here.

// Presence has been enabled; now register with the profile.
hr = pIRTCClientProvisioning->EnableProfile( pIRTCProfile, 
                                             RTCRF_REGISTER_ALL);

// If (hr != S_OK), process the error here.

// Enable presence using a file for persistent storage.
// If presence functionality is required, it must be 
// enabled immediately after registration. 
hr = m_pPresence->EnablePresence(VARIANT_TRUE, varPresence );

// If (hr != S_OK), process the error here.

// Track presence information of a presentity.
hr = m_pPresence->AddBuddy( bstrBuddyURI,
                            bstrBuddyName,
                            bstrBuddyData,
                            VARIANT_TRUE,
                            NULL,
                            0,
                            &m_pBuddy );

// If (hr != S_OK), process the error here.

Visual Basic Code Example

'Set the error handling routine here. 
' On Error GoTo MyErrorRoutine 

Public g_objPresence As IRTCClientPresence
Dim BuddyURI    As String
Dim BuddyName   As String
Dim BuddyData   As String
Dim FileName    As Variant

BuddyURI    = "someone@example.com"
BuddyName    = "My Buddy"
BuddyData    = "Any data here"
FileName    = "PresenceFile.xml"

' Get the Presence interface from the RTC Client object.
Set g_objPresence = g_objRTCClient

' Automatically add a watcher to the list of allowed watchers.
' There is no need to fire an event.
g_objPresence.OfferWatcherMode = RTCOWM_AUTOMATICALLY_ADD_WATCHER

' Presence has been enabled; now register with the profile.
Call g_objProvisioning.EnableProfile(g_objPC2PhProfile, RTCRF_REGISTER_ALL)

' Enable presence using "PresenceFile.xml" for storage.
' If presence functionality is required, it must be
' enabled immediately after registration.
Call g_objPresence.EnablePresence(True, FileName)

......

' Track the presence information of a presentity.
Call g_objPresence.AddBuddy(BuddyURI, BuddyName, BuddyData, True, Nothing, 0)