Library

Sending Instant Messages via Mashup Server

Archived Content
This article is provided for historical perspective only, and may not reflect current conditions. Please refer to relevant product page for more up-to-date product information and resources.
  • By Yumani Ranaweera
  • 9 Jul, 2008

Applies To :

WSO2 Mashup Server v1.0.2 onwards
Sun JDK v1.5

 

Introduction

WSO2 Mashup Server

WSO2 Mashup Server is a product created to support developers to create and deploy java script Web Service mashups. It makes it easy to deploy a Web Service, and also to consume and combine information from other Web Services and expose the resulting mashup as a service. As mentioned in the Mashup Server documentation also in detail, the server uses web services as its mechanism for acquiring data, XML as its central abstraction, Javascript with E4X extensions for manipulating that data and again Web Services as the primary way to re-publish the new information stream, with bridges to HTML, RSS, and other output mechanisms.

On the web mashup means consuming information published from multiple sources and integrating them into a new information stream. This is a a part of the Web 2.0 trend about decentralizing the power to the web consumer where it allows them to build their own customized view by mixing information from more than one source.

There are many samples shipped with WSO2 Mashup Server installation which illustrate this concept further. For example sample TomatoTube, (The Making of TomatoTube, Featuring The WSO2 Mashup Server), is taking information about movie reviews from Rotten Tomatoes site and the trailers of them from another community driven site, YouTube. It then gives the top 10 rated movies along with their trailers as a web service.

 

Instant Messaging

Instant messaging, as most of us already know is a capability which offers real-time communication and allows easy collaboration across Internet. WSO2 Mashup Server from its 1.0.2 release introduced a capability for the javascript web service writer to include instant messaging in their web services. Through this it allows the instant messages to be sent via the java script services that you deploy in the Mashup Server. The instant messaging protocols supported by the WSO2 MAshup Server are MSN, AIM, ICQ, Jabber and Yahoo. Mashup Server provides this capability through a built-in object called IM host object.

The business scenario behind this is: say that you have a web service where you capture the latest academic information of an acadamic course that your following, and if there is an instance where a notice of an assignment submission date pops up, we can have our mashup to send us an instant message when such a notice occure and then to further configure it to keep up remind about the date until we disable it. Like wise there could be scenarios where somebody would need alerts or instant messages sent about weather updates, stock market statuses, country situation etc., sent to their PC while they are at work or study.

Blending this capability with various other host objects such as session, system, feed, APP, WSRequest, scraper, file, email etc., which the server is providing, you can create web services which meets your exact day to day information retrieval needs.

 

Table of Contents

  1. Prerequisites
  2. First Steps
  3. Scenario 1
  4. Scenario 2
  5. Scenario 3
  6. Scenario 4
  7. Scenario 5
  8. Conclusion

Pre-requisites:

  • Mashup server installation
  • Instant messaging protocols listed below;
  1. Yahoo Messenger
  2. MSN
  3. AIM
  4. Jabber
  5. ICQ

TIP: To support the Yahoo protocol, you need to have the Yahoo Instant Messenger & Chat Protocol library copied onto the Mashup Server's lib folder. Please note that you will need to restart the server after doing so.

First steps:

  1. Download and install WSO2 Mashup Server version 1.0.2 or above.

    Unzip the downloaded .zip distribution to your file system.

  2. Create an admin user account.

    Go to the MASHUP_HOME/bin folder of your installation and execute the startup.bat/.sh file to start the server.

    Access the admin console via https://localhost:7443

    At the very first access the WSO2 Mashup Server admin console will prompt you to create an admin user. Complete the initial registration by following the on-screen tips.

TIP: Once the user account is created, you will have a folder inside MASHUP_HOME/Scripts in the name that you used to create the above user account. Thereafter, every service you create from this user login will be stored in this folder. See the image below:

folders

Image 1: Mashup sever folder structure with the user specific repository created.

In our tests, we will deploy all our services under the user account we just created. In Mashup server there are two ways to create and deploy a service:

  1. Create and deploy a Java script Web service from the Mashup Server UI.
  2. Copy the Java script Web service directly to the file system, from which the server will pick up Web service for deployment.

I will use the second approach which limits UI interactions, which is also the faster approach. So lets move on to the test scenarios. We will start with the basics and move on to more complex scenarios.

The Mashup code segments are already made available in this tutorial. Please change the <SENDER_ID>, <PASSWORD>, <RECEIVER_ID> tags to reflect correct user information, before you deploy the services.

Scenario 1: Sending a Message via Yahoo Messenger.

In this test, we will login to Yahoo messenger to send a message to another Yahoo user. After the message is sent we will disconnect the session.

Now, with your server started, copy the code below onto a note pad document, change the tagged information and save the file as yahootest.js into the user account folder that we just created. If you check the server log at this stage, you will see the yahootest.js Web service being deployed:

yahoo.inputTypes="none";
yahoo.outputType="string";
function yahoo(){
var im = new IM("yahoo");
im.login("<SENDER_ID>","<PASSWORD>");
im.sendMessage("<RECIEVER_ID>","test hi");
im.disconnect();
return "successful";
}

Code Listing 1: Sending a message to Yahoo user

Now access the ?tryit page from https://localhost:7443/services/<user account>/yahootest?tryit. This is the ?tryit page of our Web service. (Since this scenario is using the Yahoo messenger, you need to have the lib folder updated with the required jar file before accessing the ?tryit page for the Web service deployed.)

NOTE: ?tryit page is a dynamic client UI found in products such as WSO2 Mashup Server and WSO2 WSAS for the purpose of testing your Web service.

The tryit page will appear as below;

tryit1

Image 2: Tryit page for scenario 1

Click on the 'Yahoo' button to send your message. You will notice a message “test hi” is received at the receiver’s end.

NOTE: You can try the same code to MSN, Jabber, ICQ and AIM also by creating an instance of that messenger in the code (e.g. var im = new IM("aim") )

TIPS: We can enhance the look and feel of the service by adding html code. Readability of the ?tryit could be improved via mashup server's .documentation annotation. Steps of doing these can be found in Mashup Server Document index.

Scenario 2: Sending a sequence of messages via Jabber.

This scenario checks if a sequence of messages are successfully received at the receiver's end.

Deploy the code given below as a Web service in your mashup server, just as we did in the earlier scenario.

jabberManyMessages.inputTypes="none";
jabberManyMessages.outputType="string";
function jabberManyMessages(){
var im = new IM("jabber");
im.login("<SENDER_ID>@gmail.com","<PASSWORD>");
im.sendMessage("<RECIEVER_ID>@gmail.com","test hi");
im.sendMessage("<RECIEVER_ID>@gmail.com","how are u");
im.sendMessage("<RECIEVER_ID>@gmail.com","this is a test message");
im.sendMessage("<RECIEVER_ID>@gmail.com","this is a test message1");
im.disconnect();
return "successful";

}

Code Listing 2: Sending a sequence of messages via Jabber

After you have successfully deployed the service, access its tryit and try the operation. You should observe whole tale of messages you sent being received at the receivers end.

NOTE: One problem here is that we don’t maintain the same sending sequence at the receiver’s end.

Scenario 3: Sending a sequence of messages via two messengers without having to disconnect.

In this scenario we login to Jabber and send a sequence of messages. Without disconnecting Jabber, we send messages via Yahoo within the same service.

jabberManyMessagesWithNoDisconnect.inputTypes="none";
jabberManyMessagesWithNoDisconnect.outputType="string";
function jabberManyMessagesWithNoDisconnect(){
var jabber = new IM("jabber");
jabber.login("<SENDER_ID>@gmail.com","<PASSWORD>");
jabber.sendMessage("<RECIEVER_ID>@gmail.com","test hi");
jabber.sendMessage("<RECIEVER_ID>@gmail.com","how are u");
jabber.sendMessage("<RECIEVER_ID>@gmail.com","this is a test message");
jabber.sendMessage("<RECIEVER_ID>@gmail.com","this is a test message1");


var yahoo = new IM("yahoo");
yahoo.login("<SENDER_ID>","<PASSWORD>");
yahoo.sendMessage("<RECIEVER_ID>","test hi");
return "successful";
}

Code Listing 3: Sending a sequence of messages.

Deploy the service and access the operation via ?tryit. You will notice that messages from both Jabber and Yahoo are received from the receiver's end.

Scenario 4: Sending emicons

In this scenario, we check for the stability of the instant messaging feature when emicons are being added to a message.

yahooWithEmicons.inputTypes="none";
yahooWithEmicons.outputType="string";
function yahooWithEmicons(){
var im = new IM("yahoo");
im.login("<SENDER_ID>","<PASSWORD>");
im.sendMessage("<RECIEVER_ID>","Hi from yahooWithEmicons :) :p");
im.disconnect();
return "successful";
}

Code Listing 4: Sending emicons

Deploy the service and access the operation from service's ?tryit and check the results. The the messages with emicons should be sent without a problem.

Scenario 5: Broadcast messages

In this scenario we check the broadcasting of messages via a given messenger.

BroadcastingYahoo.inputTypes="none";
BroadcastingYahoo.outputType="string";
function BroadcastingYahoo(){
var im = new IM("yahoo");
im.login("<SENDER_ID>","<PASSWORD>");
im.sendMessage("<RECIEVER_ID1>","Hi! This is to test IM broadcasting frm Mashup server :);
im.sendMessage("<RECIEVER_ID2>","Hi! This is to test IM broadcasting frm Mashup server :);
im.sendMessage("<RECIEVER_ID3>","Hi! This is to test IM broadcasting frm Mashup server :);
im.disconnect();
return "successful"; }

Code Listing 5: Broadcasting messages

Deploy the service and access the operation from the service's ?tryit and check for results. The messages should be sent to all receivers without a problem.

NOTE: For all above scenarios, you can skip the point where you need to add username and password of the sender by having those values in the server.xml that resides in MASHUP_HOME/conf folder. The default server.xml has pre-populated dummy values. We need to update these values with actual username and password values. I have pasted the default configuration below for ease of reference.

serverxml

Image 3 : IM configurations in server.xml

Conclusion/Summary

In this tutorial we looked at few sample scenarios that demonstrated the instant messaging features in the WSO2 Mashup Server such as:

  • Sending a single messages to a messenger user.
  • Sending a sequence of messages to a messenger user.
  • Sending a sequence of messages to more than one messenger type via the same service without having to disconnect inbetween.
  • Sending emicons via a service.
  • Broadcasting messages to several receivers.

References

  1. WSO2 Mashup Server User Documentation
  2. WSO2 Mashup Server wiki pages
  3. JavaDoc for the IM host Object

Author

Yumani Ranaweera, Senior Software Engineer, WSO2 Inc. yumani@wso2.com

About Author

  • Yumani Ranaweera
  • Senior Technical Lead
  • WSO2