Pages Menu
TwitterRssFacebook

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

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

This is one post in a series of more than 30, devoted to helping you get up to speed quickly with the new Skype Web SDK. Each lesson comes with source code (in GitHub) and a working demo page so you can see what it does. Check out the full list of posts on the Skype Web SDK page.


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).

[code language=”javascript”]

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

[/code]

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:

[code language=”javascript”]

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

[/code]

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

[code language=”javascript”]

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

[/code]

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!

[code language=”javascript”]



[/code]

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.

Disclaimer: This is sample code, intended to inform and educate. It is not production-ready and is lacking key components such as error handling. You use it entirely at your own risk. You should fully understand the effects, limitations and risks of the code before executing, and understand the implications of any set-up steps. By using these code examples you are using the Skype Web SDK, so you should read the Skype Software License Terms to which you are agreeing.

Good to Know

Post a Reply

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

This site uses cookies to help make this website better. By continuing to use this site we’ll assume you’re OK with that (implied consent).