Sending Instant Messages via Mashup Server
By Yumani Ranaweera
- 9 Jul, 2008
Applies To :
|WSO2 Mashup Server||v1.0.2 onwards|
WSO2 Mashup Server
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.
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
- Mashup server installation
- Instant messaging protocols listed below;
- Yahoo Messenger
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.
- Download and install WSO2 Mashup Server version 1.0.2 or above.
Unzip the downloaded .zip distribution to your file system.
- 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:
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:
- Create and deploy a Java script Web service from the Mashup Server UI.
- 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:
var im = new IM("yahoo");
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;
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.
var im = new IM("jabber");
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");
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.
var jabber = new IM("jabber");
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");
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.
var im = new IM("yahoo");
im.sendMessage("<RECIEVER_ID>","Hi from yahooWithEmicons :) :p");
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.
var im = new IM("yahoo");
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 :);
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.
Image 3 : IM configurations in server.xml
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.
Yumani Ranaweera, Senior Software Engineer, WSO2 Inc. firstname.lastname@example.org
- Yumani Ranaweera
- Senior Technical Lead