After writing article about Introduction of Server Sent Events with real life PHP SSE
example I decided to write another article this time about using Server Sent Events in
Java and Tomcat. Before to start I strongly recommend you to read first article and look
into PHP code, because this time I will not describe how Server Sent Events (SSE) works.
In short idea is to write simple Java chat application. So Java will be used in a
back-end with just one Servlet that runs on Apache Tomcat or any other Servlet
compatible container. Front-end will be using JavaScript and just few lines of
HTML and CSS.
As you know or may not know - SSE is one way communication from Server to Browser.
Clients subscribes to URL to listen to. And when there is something to send server
send it. But here comes first problem. Web Chat is a two way communication. Users types
something that is send to other users. For out simple purposes this could be achieved
using AJAX calls. But let’s start with example step by step and you will get the idea.
Java Chat Message Class
Chat Servlet
Next step is to write Java Servlet that will handle requests and send responses to clients.
This Servlet will need of few properties. First attribute counter is used to give unique
ID’s to Messages. But in real life this will be done in Database. we need to start a Thread
inside of Servlet so property running is about it. Also we need to store all open connections
from browsers in order when new message arrives to send it to all of them. Also we need to
store somewhere messages when they arrive. And at the end we need to simple message store
to store messages. Note that in real life this could be a Database. But for our demo purposes
this is simple in memory storage. And here is the code.
Next step is to write an inner Thread that processes and send messages to clients - all open connection
from browsers. Here how it looks like. Note that storing messages in this way is not what you want in
real scenario. It is here only for demo purposes.
Next step is to write doGet method that handles incoming users that want to join the
chat. Here is a code. Note that we need to add some Tomcat specific code on line 13 that
may be not necessary if you are using other Servlet container. Also first lines check if
user arrives for the first time on the page and if it is so then it redirects to given JSP
page. This could be done in a different way - with different Servlet or to goes directly
to JSP.
The next important thing is to set correct HTTP headers. Then we need to check for last
message id. If by some reason connection was dropped then browser reconnects automatically
with in given time. Which is by default 3 seconds. But we set it in 1 second by this command
in code response.getWriter().println("retry: 1000\n"). Last part is to start asynchronous
context and to override it’s methods - mainly to remove it from the map of open connection
when connection is dropped by some reason.
Here the code how looks like:
Next step is to write a doPost method that handles incoming messages. We said that those
messages are send via AJAX calls from browsers. And here is a simple method that handles
them. First we set character encoding of request. Note that in real scenario this should
happen in some filter. But for simplicity we do not have it. Note that this is very simple
and basing processing. You may want to collect and store more info - such as time-stamp,
IP address of sender, user name if applicable, etc. Also messages may be saved in
Database here.
And the last is to write a small simple method that sends message to client and to
override init and destroy methods.
JavaScript code to handle SSE
At the end you need to write some JavaScript code that handles this. Here is the
code - it is simple and straightforward implementation that just adds the message into
one scrolabble DIV element.
AJAX call to send message to server
And here is JavaScript AJAX method that sends message to the server. It is called
when user clicks button.
Show me example
And here is the real demo where you can play,
download and study code.