Is there any Spring based framework to send notifications to the web page. I have seen http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_sse I am also looking into something that can support most of the browsers. Is there any framework or add-on in Spring for this functionality for the server-side code? And any jquery framework to support this for the browser?
TIA.
I have used the "long polling" method. You basically make an ajax request to the server for data on page load. The server waits until data is available before it responds. On the client and server, you can make the request timeout every 30 seconds or so to avoid having too many threads running on the server. The client just reissues the request after timeout.
This site provides a good introduction to long polling using jQuery.
Spring doesn't really have any explicit features that support this (e.g. pooling the polling threads) AFAIK, but you may look into the new async support in Spring MVC 3.2
You can write your own servlet as below, for further information refer link. As this works with servlet this might work with spring mvc controllers also.
import java.io.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class sse extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try
{
System.out.println("SSE Demo");
response.setContentType("text/event-stream");
PrintWriter pw = response.getWriter();
int i=0;
while(true)
{
i++;
pw.write("event: server-time\n\n"); //take note of the 2 \n 's, also on the next line.
pw.write("data: "+ i + "\n\n");
System.out.println("Data Sent!!!"+i);
if(i>10)
break;
}
pw.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
{
doPost(request,response);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With