Accueil > Jetty, WebSockets > WebSockets with Embedding Jetty [step1]

WebSockets with Embedding Jetty [step1]


Since several years, WEB Application are more and more improved by using AJAX (Asynchronous JavaScript Technology and XML) to provide the same features than a Fat client (Swing, SWT Application). With AJAX, the client WEB Browser can communicate to server side with Javascript and can refresh only a fragment of the WEB Page and not the all page. A good sample of this feature is the GTalk which is the Google GMail Chat Application.

Chat Application with XMLHttpRequest (AJAX)

AJAX is based on XMLHttpRequest which is W3C specification :

The XMLHttpRequest specification defines an API that provides scripted client functionality for transferring data between a client and a server.

Here a screen which shows you communication between client and server for GTalk Chat Application with AJAX :

This schema shows you that there are 3 communications :

  • [1] : Chat Application 1 send hello message.
  • [2] : Chat Application 2 poll the server to check if there are received messages.
  • [3] : Chat Application 2 receives the message (response of the [2] communication).

You can see that in the Chat Application context with AJAX, there are a lot of communications between client and server side to check if there are received messages ([2] communication). WEB browser client must check every time if there are new received messages by polling the server (ex : the WEB Browser send every 5 seconds a request XMLHttpRequest to the server side to check if new messages are received). This solution can deteriorate the server performance because there are a lot of request just to check if they are new received messages (if they are no received messages, the response of this communication is not used).

Chat Application with WebSockets

To avoid a lot of communications between client and server side, the solution is that server should send a message to the client side as soon as it receives message without the client side polls the server. For his feature, HTML5 specify a new component called WebSockets:

This specification defines an API that enables Web pages to use the WebSocket protocol for two-way communication with a remote host.

Here a screen which shows you communication between client and server for GTalk Chat Application with WebSockets :

This schema shows you that there 3 comunications :

  1. [1] : Chat Application 1 send hello message
  2. [2] : Chat Application 2 receives the message. The client NOT need to poll the server to check if new messages are received.

Chat Application Implementation

WebSockets features are cool, but how to implement that? I will try to explain step by step how to develop a Chat Application on client/server side which will work in any JEE Server. I will use Google Chrome, a WEB Browser which supports WebSockets, to test the Chat Application.

Chat Application Implementation

To implement a Chat Application with WebSockets you need to manage :

  • WebSockets on client side. For that the WEB Browser must support WebSockets which is a Javascript object. Today Google Chrome implements this component. I have not studied the other WEB Browser which support WebSockets.
  • WebSockets on server side. When I have started to study WebSockets, this point was very difficult for me because there is not a real specification. Each JEE Server implements WebSockets with their own solution (I think Servlet 3 specification should resolve that). Today it exists several solutions but for Java the only solution that I have done with success is Jetty Server (I was not able to to manage WebSockets with Grizzly (Glassfish) for instance). To manage WebSockets on server side, the server must manage asynchronous HTTP response.

I was able to manage WebSockets only with Jetty Server. But how to manage WebSockets with another JEE server? Indeed in professional job, the JEE server is often forced (in my case I must use WebSphere -(). I know that the goal of Atmosphere is to support the HTML5 WebSockets specification, but I have the impression this Framework use native WebSockets implementation of the JEE Server.

To manage WebSockets in any JEE Server, I have found a solution by using Embedding Jetty. Embedding Jetty means that you can create, start and stop a Jetty Server with Java code. So you can create, start and stop a Jetty Server with few code in a Java main. The solution is very simply and I would like share you my idea with articles called WebSockets with Embedding Jetty (you can find the plan in this link) by explaining step by step how to manage a Chat Application with Jetty-WebSockets and use a Tomcat Server as JEE Server. I will explain you :

  • how to create Chat Application on client side. I will use Google Chrome to test the application.
  • how to create Chat Application on server side with Jetty-WebSockets.
  • how to start the Chat Application on server side with Java main with Embedding Jetty.
  • how to integrate Embedding Jetty in another JEE Server. I will use Tomcat.

This idea follow the same idea than WebSockets for GWT with the JettyLauncher.

Why I love Jetty?

Before starting WebSockets with Embedding Jetty articles, I would like just tell you why I love Jetty Server :

  • Jetty is light (like Tomcat).
  • Jetty can be used in several context :
    1. as JEE Server.
    2. in OSGi context.
    3. in a Java Main with Embedding Jetty.
  • Jetty can be configured with several means :
    1. Jetty XML files.
    2. Spring XML files.
    3. Java code. (Embedding Jetty)
  • Jetty Tools :
    1. Tools for OSGi context. Eclipse RAP use Jetty by default to start the Application.
    2. Tools for standard JEE : Jetty WTP Adaptor can be used in a Eclipse WTP to start/stop debug WEB Application deployed in a Jetty Server.
  • Jetty support WebSockets.
  • For JUnit Tests WEB components (Servlet, Filter, Web Services….), Embedding Jetty can be used to create JUnit Tests (to start a Jetty server and do the tests). For instance Apache CXF uses Embedding Jetty to tests WebServices, Rest services….
  • Jetty is Open Source (Eclipse)
  • Jetty Team is very enthusiasm and the Jetty the community is responsive.

Conclusion

In this article we have seen how WebSockets could be used in a Chat Application and introduce WebSockets component on client/server side. In the next articles, I will explain you step by step how to develop Chat Application on client side (in the [step2]) and server side with Jetty-WebSockets (in the [step3]). Embedding Jetty will be used to start/stop the server with Java main (no need to install a Jetty Server).

Catégories :Jetty, WebSockets
  1. Joerg
    février 2, 2012 à 4:22

    Thanks for the tutorials – please see how WebSphere supports Web Messaging: http://publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=%2Fcom.ibm.websphere.web2mobile.webmsg.help%2Fdocs%2FOverview.html
    Best wishes
    J.

  1. juillet 24, 2011 à 5:36
  2. juillet 25, 2011 à 10:44

Laisser un commentaire