Any Spring web push notifications, where can I get a notification updated on a web page without refreshing the page?

Is there any Spring framework for sending notifications to a web page. I saw http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_sse I also study what most browsers can support. Is there any structure or addition in Spring for this functionality for server-side code? And any jquery framework to support this for the browser?

TIA.

+6
source share
3 answers

I used the "long poll" method. You basically make an ajax request to the server for page load data. The server waits until data is available before it responds. On the client and server, you can timeout the request every 30 seconds or so to avoid too many threads running on the server. The client simply reissues the request after a timeout.

This site provides a good introduction to a lengthy survey using jQuery.

Spring doesn’t actually have any explicit functions that support this (for example, combining polling streams) AFAIK, but you can peek into the new asynchronous support in Spring MVC 3.2

+5
source

You can write your servlet as shown below, for more information see the link. Since this works with a servlet, it can work with spring mvc controllers.

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); } } 
+1
source

My way to use it would be with javascript setInterval() along with a jQuery AJAX call to receive notifications. in the document is ready, call this (in jsp): setInterval(callMe, 30000)

in the JS file:

 function callMe(){ ... $.ajax({ type: "GET", url: "ajaxNotifications", success: function(count){ alert( "You have: " + count + "notifications now" ); //stick it to #element or DIV or .class where ever you want. } error:function(){ ... } }); } 

and finally in the Spring controller:

 @RequestMapping(value="/ajaxNotifications", method = RequestMetho.GET) public @ResponseBody String getMeCount(HttpServletReuest req, HttpServletRespones resp){ //get the desired values from req ... //fire a reuest via service layer to the database for getting notifs number ... //Don't forget to return it as a String, be it error or count. } 

Hope this gives you some idea or helps someone else find the answer to this question. :)

0
source

Source: https://habr.com/ru/post/923055/


All Articles