Reputation: 54
I need to show notifications on booking by client events to some other user. I want to do it in a facebook-like style: whenever someone makes a booking through my portal, I want that booking to be shown as a popup to another user who is using my application.
How can I achieve this? I'm using Spring 3.0, JSP and jQuery.
Upvotes: 1
Views: 9937
Reputation: 10043
As said in comments, you can either go with long polling (more or less means: client sends a request to server and it is kept open until the server responds with an update, and then as soon as a response received client send another request) or short polling (every X seconds your client pings a request to your server to ask for updates, server responds immediately with yes/no).
Web sockets are growing in popularity/support but not supported pre ie10 so depends on what browsers you want to support (http://en.m.wikipedia.org/wiki/WebSocket)
Short polling is incredibly simple with jquery, and can just ajax a request to your server at whatever refresh rate.
EDIT
If you are not going for something like websockets (because you have to support older browsers for example), then I would recommend using short polling rather than the traditional long polling approach such as comet.
See here for a succinct long vs short polling commentary: https://stackoverflow.com/a/4714111/258813 Basically, long polling is useful if people need to be notified immediately, but is more complex and memory intensive - it means every logged in user will have an open connection to the server so you will be limited in the number of open connections your server will support.
Short polling is currently probably the most common approach to solving the problem you describe - if you go to twitter for example and open up chrome developer tools/firebug (whatever dev tools you have on your browser) and wait a few seconds you will see the ajax requests being made regularly in the background checking for updates.
If you can cope with a few seconds lag in notification, then short polling is really simple, here is a basic example:
On page load, call this in your JS:
setInterval(checkForNotifications, 10000);
The above will call a function checkForNotifications()
every 10 seconds (obviously, you can change the 10 seconds to any length of time - just adjust the 10000 in the above line according to how regular you want users to be updated).
Then, in your checkForNotifications
function, just make an ajax call to your server asking if there are any updates - if the server says yes, then just display the alert using JS, if not (which will be most of the time most likely) then do nothing:
function checkForNotifications(){
$.ajax({
url: "your/server/url",
type: "GET",
success: function( notification ) {
//Check if any notifications are returned - if so then display alert
},
error: function(data){
//handle any error
}
});
}
Upvotes: 4