Pages Menu
TwitterRssFacebook

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

Learn Skype Web SDK Day 15 : Receiving Instant Messages

Learn Skype Web SDK Day 15 : Receiving Instant Messages

[contemplate-1]
View Demo

Once you’ve created and started a conversation using Skype for Web API, you actually don’t need to worry about how you’re going to receive IM messages. Skype for Web will make sure that you receive messages sent to the conversation. The issue instead becomes one of “how do I found out when that’s happened”.

Each conversation has a historyService object. This provides a collection of all messages in the conversation: incoming, outgoing, joining, leaving. IM messages have a specific type of message called a TextMessage. By listening for messages of type TextMessage we can pull out instant messages being sent in both directions.

Of course, you probably already know when you’re sending messages to the conversation, but having them in the historyService object means that you have one centralised place to watch to track the conversation. If you’re writing a user interface for an IM conversation, it’s much easier to use the historyService object, where messages are ordered in the order in which they happen, than it is to fake the history when sending, which risks getting the timing wrong.

The historyService contains some useful members to make retrieiving and staying up to date with the conversation easier. The activityItems collection is the actual list of conversation history items. To react everytime something is added to this collection, register a listener on activityItems.added:

[code language=”javascript”]
conversation.historyService.activityItems.added(function (newMsg) {
//do something with newMsg
});
[/code]

Items in the activityItems collection have the following members and methods describing the conversation item. (there are others, but I’ve pulled out the interesting ones):

  • newMsg.key – a unique GUID which you can use to identify the message
  • newMsg.isRead – a boolean value. Set to false initially, but you can mark messages as read to make parsing for new messages easier if needed.
  • newMsg.direction() – either ‘Incoming’ or ‘Outgoing’ depending on the direction the message is travelling in
  • newMsg.text() – the plaintext version of the message
  • newMsg.html() – an HTML formatted version of the message, in a span tag
  • newMsg.timestamp() – timestamp of the message
  • newMsg.sender.uri – the sip address of the sender
  • newMsg.sender.person.displayName() – the display name of the sender, if it’s known. If not, the URI is shown instead.

You also have access to the entire person object (newMsg.sender.person) which you can use to subscribe to presence, get more information etc.

In the code sample below, the signed in user can send and receive instant messages with another user:

[code language=”javascript”]


Conversation State:




    [/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.
    [contemplate-2]

    Post a Reply

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