Sending and Receiving Messages (JavaScript Library)

Ff750612.note(en-us,MSDN.10).gifNote:
Current information about Live Connect is now available in the Windows Live Developer Center. The information in the following sections is provided for legacy purposes only.

You can give users the ability to send messages to any member of their Windows Live Messenger contact lists. To do this, you need to incorporate the following functionality in your application.

There are three types of messages that you can send, as defined by the Microsoft.Live.Messenger.MessageType enumerator:

  • Application message - An application-specific message.
  • Nudge message - Used to send a "nudge" message.
  • Text message - Used to send a traditional Messenger text message.

This topic covers sending text and nudge messages. For more information about application messages, see Sending and Receiving Application Messages (JavaScript Library).

To send a text message
  1. Create a new instance of Microsoft.Live.Messenger.TextMessage.

    function sendAMessage(msgText, cid) {
        var color = Microsoft.Live.Messenger.TextMessageColor.FromRgb(42, 128, 67);
        var format = new Microsoft.Live.Messenger.TextMessage.Format(color, "Verdana", Microsoft.Live.Messenger.TextMessageStyle.regular);
        var message = new Microsoft.Live.Messenger.TextMessage(msgText, format);
    }
    
  2. Call sendMessage on the messengerContext instance, passing the CID for the intended recipient and the message instance.

    messengerContext.sendMessage(cid, message);
    
To send a nudge message
  1. Create a new instance of Microsoft.Live.Messenger.NudgeMessage.

    var message = new Microsoft.Live.Messenger.NudgeMessage();
    
  2. Call sendMessage on the messengerContext instance, passing the CID for the intended recipient and the message instance.

    messengerContext.sendMessage(cid, message);
    
To handle incoming messages
  1. Subscribe to Microsoft.Live.IMessengerContext.OnMessageReceived. To do this, call the add_onMessageReceived accessor function on the messenger context instance.

    messengerContext.add_onMessageReceived(onMessageReceived);
    
  2. Create a handler function to get the data from each message that is received.

    function onMessageReceived(sender, e) {
        var message = e.get_message();
        var msgText = message.get_text();
        var timestamp = message.get_timestamp();
    }
    
Show: