0

I am using java and jsp for my project. I have developed a facebook like notification icon. I am doing a servlet URL call by jQuery. Servlet checks the value in DB and return the result. This call interval is 1 second. Ex.-

<div id="noti_Container">
    <a href="layoutMailbox.jsf"> 
        <img src="#{resource['images:Network.png']}"
        alt="Notifications" style="width: 25px;" />
</a>

<div id="check" class="noti_bubble">2</div>
    <script>
        var int=self.setInterval(function(){clock()},1000);
        function clock()
        {
            $.get('Servlet URL', function(responseText) {
                    document.getElementById("check").innerHTML = responseText;
                    });
        }
    </script>
</div>

But this could be a "heavy" approach because it pings Servlet every second and doing the DB-interaction.

Is there any alternative to update count of Notifications if DB gets updated?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
theGamblerRises
  • 686
  • 1
  • 11
  • 27

2 Answers2

1

You can look into ajax push solutions like cometD or websocket (socket.io). Using it you will 'push' data to the web browser if DB changes instead of pooling for changes. Personally i tried websocket with good results but there can be issues with browser support.

Edit:

Check this article about long polling. Just bumped into article about using JSR 356 (WebSocket). There is similar question on stackoverflow: how to implement facebook like notification?

Community
  • 1
  • 1
kaos
  • 1,598
  • 11
  • 15
1

If you are sure that only the Java-Webapp conntects to the SQL-Database only, you better use the java.util.Observable and his GoF-Pattern, instead of ask the SQL-Server for a value you added in the last 1 second.

You also should not use $.get. Please use

function clock() {
  $('#check').reload('Servlet URL');
}
var int=self.setInterval(clock, 1000);

Because you are reloading an element by url. The Overhead is not necessary.

Grim
  • 1,938
  • 10
  • 56
  • 123