Question

Im working on web application using the following stack of technologies: Spring, Hibernate, JSP. I have a task to make one of user social element - messages. As standard of implementation message system i take facebook system. On of the problem i faced is a polling server every 1-5 seconds (what period i have to take?) to retrieve an information about unread messages. Also i want to polling server to retrieve new messages at conversation page (like a chat). What i did:

Example code of get count unread messages.

Server side:

@RequestMapping(value = "/getCountUserUnreadMessages", method = RequestMethod.POST)
public @ResponseBody Callable<Integer> getCountUserUnreadMessages(@ActiveUser final SmartUserDetails smartUserDetails) {
    // TODO add additional security checks using username and active user
    return new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {

            Integer countUserUnreadMessages = messageService.findCountUserUnreadMessages(smartUserDetails.getSmartUser());
            while (countUserUnreadMessages == 0) {
                Thread.sleep(1000);
                countUserUnreadMessages = messageService.findCountUserUnreadMessages(smartUserDetails.getSmartUser());
            }
            return countUserUnreadMessages;
        }
    };
}

Client side:

(function poll(){                                                  
    setTimeout(function () {                                       
        $.ajax({                                                   
            type: "post",                                          
            url: "/messages/getCountUserUnreadMessages",           
            cache: false,                                          
            success: function (response) {                         
                $("#countUnreadMessages").text(response);          
            }, dataType: "json", complete: poll, timeout: 1000 }); 
    }, 3000);                                                      
})();    

So client send a request to retrieve count unread messages every second with a timeout in 3 seconds (is it good decision?).

But i think that is the worst callable code ever :-P

Tell me how to do better? What technique use?

Additional information: Yeah, i supposed that it would be highload, many users service in the Internet.

Was it helpful?

Solution

Try Spring 4 WebSocket support:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

WebSockets support full duplex communication over a dedicated TCP connection that you establish over HTTP.

OTHER TIPS

If you are expecting this application to have to scale at all I would make that timing interval more like every 30 - 90 seconds. Otherwise you are basically going to be designing your own built in DOS attack on your self.

You might look into Spring Sockets. It sounds like it's long polling option might work better for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top