WebSockets with Embedding Jetty [step1]
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 :
-  : Chat Application 1 send hello message.
-  : Chat Application 2 poll the server to check if there are received messages.
-  : Chat Application 2 receives the message (response of the  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 ( 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 :
-  : Chat Application 1 send hello message
-  : 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 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.
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 :
- as JEE Server.
- in OSGi context.
- in a Java Main with Embedding Jetty.
- Jetty can be configured with several means :
- Jetty XML files.
- Spring XML files.
- Java code. (Embedding Jetty)
- Jetty Tools :
- 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.
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).