Wednesday, June 11, 2014

Playing with WebSockets

WebSockets is a great way to build client-server applications where full-duplex communication is possible. That means, once a WebSocket client is connected to a WebSocket server, the server gets the capability of pushing messages to the client side without employing traditional hacks such as AJAX and long-polling at the client side. WebSockets specification has started as a part of HTML5 and now its running independently as I know. 

There is a nice implementation of WebSockets as a library which I tried today. It is Autobahn project which provides implementations of WebSockets for various platforms and languages such as Python, JavaScript, C++ and Android/Java. As the first step of exploring the capabilities and features of Autobahn library, today I ran the echo client / server example of the Autobahn project. Here are the basic steps I followed. 

The echo server example of the Autobahn project is in its Python based project while I'm going to use the Android based echo client for communicating with that server. First of all we install the Python based Autobahn project on the Ubuntu 12.04 system. Goto the AutobahnPython project in Github, take a clone of the source code, do some installation stuff and at the end, run the sample echo server.

git clone https://github.com/tavendo/AutobahnPython.git

cd AutobahnPython
git checkout v0.8.5

cd autobahn
sudo python setup.py install

cd ../AutobahnPython/examples/twisted/websocket/echo
python server.py

Now, the echo server must be up and running. It's time to prepare the client for Android. Goto the AutobahnAndroid project in Github and take a clone of the source code as follows.

git clone https://github.com/tavendo/AutobahnAndroid.git

Open the Eclipse IDE (which you must have already configure with Android SDK tools) and select the Autobahn directory from the import project window. It will show many example projects together with a project called autobahn are ready to be imported. Import them to the workspace. Among other example projects, EchoClient project is what we need. If Eclipse complain about some missing build paths and stuff, fix those carefully. In my case, two library Jar files were missing. So, I right-clicked on EchoClient project and took the properties option. In the Properties window, I went to the Java build path option and from there, went to the Libraries tab. Two Jar files were shown as missing. I selected each one and then clicked the Edit button. There I could browse through the autobahn project and selected the relevant missing Jar files. Clicked OK and then things were fine. 


Now I connected my Android device which runs Android 4.3 and then using Eclipse, I built and installed EchoClient app on the device. It's time to turn the client on. I need my Ubuntu machine and Android device to be in the same wireless LAN for this task. So, I created a WiFi access point on Ubuntu machine and connected the Android device to the access point. Now, in the EchoClient app on the Android device, I had to input the IP address of the Ubuntu machine where the echo server runs and click the Connect button. Once they are connected, type a message to be sent from the client to the server and click the Send button.

The message should be sent to the echo server and then echoed back to the client which will be displaced on the Android device as a pop-up message. First figure shown is the output of the echo server on the terminal of the Ubuntu machine while the second figure depicts a screenshot of the EchoClient app.

This is the beginning of the exploration of WebSockets for me.  Lets see what we come across in the future.

No comments:

Post a Comment