Pages Menu

Posted by on May 19, 2015 in Learn Skype Web SDK | 1 comment

Learn Skype Web SDK Day 14 : Sending Instant Messages

Learn Skype Web SDK Day 14 : Sending Instant Messages

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

Once you’ve found the person you want to start a conversation with and set up and started the conversation – actually sending messages is a one-liner. However, the reason that it’s got it own section is that you can’t just send messages into a conversation once you’ve started it.

A newly created conversation will start Disconnected. When you call start() it will change to Created briefly before changing to Connecting. Once it’s been accepted by the other party it will change to Connected. Once one side closes the conversation its state will change back to Disconnected. You can only send IMs whilst the state is Connected, so you need to monitor this state.

To monitor the state, register a listener on the state property of the chatService, a member of the conversation. Do this before you call start() otherwise you’ll miss vital states:

[code language=”javascript”]
var application = new Skype.Web.Model.Application;
//do sign-in process
var conversation = application.conversationsManager.createConversation();
//set up the conversation, add participants etc.
conversation.chatService.state.changed(function(newState) {
//evaluate the new state

Once the state has changed to Connected, you can then send messages, using this one-liner:

[code language=”javascript”]
conversation.chatService.sendMessage(‘message to send’);

In the code sample below, once the user is signed in they can send instant messages to another user:

[code language=”javascript”]


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

1 Comment

  1. On line 95 is an error, the SDK reacts poorly to not declared variables and breaks with an exception. The error is not easy to detect, as the errormessage tells something about an unexpected character.

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