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] : Chat Application 1 send hello message
- [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.
Lire la suite…