Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Pages Menu
TwitterRssFacebook

Posted by on May 7, 2015 in Learn Skype Web SDK

Learn Skype Web SDK Day 6 : Getting & Subscribing to Other People’s Presence

Learn Skype Web SDK Day 6 : Getting & Subscribing to Other People’s Presence

[contemplate-1]
View Demo

We’ve already seen how to get and subscribe to our own presence, but you’ll probably also want to be able to subscribe to other people’s presence as well.

If you think about it, you probably won’t have a list of SIP addresses hardcoded in your application which you want to subscribe to. Instead that list of people will come from the server, either from a user’s contact list, or search results etc. But you do have to use a person object – you can’t just supply a sip address like you can in some other APIs.

We’re going to use a simple search to find a user. We’re going to assume that only one record is ever returned. The personsAndGroupsManager object contains a way of searching for people easily by providing a string, which could be their name or SIP address (similar to how you would search for someone using the desktop client).

1
2
3
4
5
6
7
var query = client.personsAndGroupsManager.createPersonSearchQuery();
query.text(searchString);
query.limit(1);
query.getMore().then(function (results) {
results.forEach(function (result) {
// do something with the user(s) here
});

In this example I’ve limited to possible number of results returned to 1, but you can also use this to implement a user search, listing out search results.

The results contain a list of person objects, which you can then use for presence (or anything else). To just grab the presence once you can just read the value of the status:

1
2
3
person.status.get().then(function(presence) {
// do something with the presence
});

You could also subscribe to updates, in exactly the same way as you did for subscribing to your own presence updates:

1
2
3
4
person.status.changed(function(newStatus) {
// The presence status has changed. The new presence value is in newStatus.
alert('Status Changed to: ' + newStatus);
});

Be aware of doing this subscription inside the for-loop of the results though. If you do, the person object reference will be lost when the loop finished executing, and your events will be lost with it. If you want to maintain a presence subscription for a user, you’ll need to copy the person object to a longer-lived variable before hooking up to the events.

When you’re done with a subscription, you should unsubscribe it, to converse resources, in the same way that you would for subscriptions to your own presence, by calling dispose() on the subscription.

In the code example below and on the demo page, I’m only getting the presence once. Adding a simple subscription to also receive updates is left as an exercise for the reader!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<div class="form-horizontal">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="username" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
</div>
 
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-default" id="btnLogIn">Log in</button>
</div>
</div>
 
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-default" id="btnLogOut">Log out</button>
</div>
</div>
 
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="text" class="form-control" id="txtUserToSubscribeTo" placeholder="Andy Other">
<button class="btn btn-default" id="btnPresence">Get Presence</button>
</div>
</div>
</div>
 
<div>
<span id="loginStatus">...</span><br/>
<span id="presenceStatus">...</span>
</div>
 
<script type="text/javascript">
 
$(function () {
 
var client;
$(function () {
'use strict';
 
Skype.initialize({
apiKey: 'SWX-BUILD-SDK',
}, function (api) {
client = new api.application();
 
// whenever client.state changes, display its value
client.signInManager.state.changed(function (state) {
$('#loginStatus').text("Login State: " + state);
});
 
}, function (err) {
alert('Error loading Skype Web SDK: ' + err);
});
 
$('#btnLogIn').click(function () {
 
// start signing in
client.signInManager.signIn({
username: $('#username').val(),
password: $('#password').val()
}).then(function () {
//log in worked!
alert('Logged in!');
}, function (error) {
//Something went wrong.
alert(error);
});
});
 
$('#btnLogOut').click(function () {
// start signing out
client.signInManager.signOut()
.then(function () {
//log out worked!
alert('Logged out!');
}, function (error) {
//Something went wrong.
alert(error);
});
});
 
$('#btnPresence').click(function () {
 
SubscribeToUser($('#txtUserToSubscribeTo').val());
});
function SubscribeToUser(searchString)
{
var query = client.personsAndGroupsManager.createPersonSearchQuery();
query.text(searchString);
query.limit(1);
query.getMore().then(function (results) {
results.forEach(function (result) {
var person = result.result;
person.status.get().then(function(presence) {
$('#presenceStatus').text(searchString + " presence: " + presence);
});
});
}).then(null, function (error) {
alert('Error:', error);
});
 
}
 
});
</script>

Demo Online

You can try this code out against your own Skype for Business environment by going to the demo page. From here you can also download the code from GitHub if you want to host it locally, or take it and use it in your next project.
[contemplate-2]

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share to Microsoft Teams