Reputation: 1169
Take for example "gmail.com" where new mails are displayed as and when they reach user's inbox. As I've understood, the trigger comes from database/web-server which updates user's inbox page.
Can anyone suggest what will be the code to update dynamic web-pages [assuming Gmail account page is generated by Servlet like technology] without refreshing & without making an explicit request from client side (please don't consider XHR sending request to server at fixed interval because that too will be from client's side).
Can it be implemented by AJAX code involving XHR & if yes,then how?
Upvotes: 0
Views: 153
Reputation: 34563
I think the usual way to do this, at present, is with "long polling", also known as Comet. There are various specific techniques, but the basic idea is that the browser sends a request to the server, and the server just keeps the connection open until it has something to send to the browser. On Gmail, for example, the browser app might immediately request the next new message in your inbox, and the server might wait half an hour before sending it because that's how long it took for a new message to actually arrive in your inbox.
Technically this is still client-driven, but the client's "request" is really just offering the server an opportunity to send data to the client at a future time of its choice.
When you use long polling, your server will have lots of pending requests sitting idle most of the time. Version 3 of the Servlet API introduced "asynchronous support", which lets a thread put a request on hold and handle other requests while the first one waits. Servlets written for older API versions won't scale so well, because the service()
method mustn't return until it's produced the response, so it has to tie up a thread just waiting until it's time to respond. Alternatively, Tomcat has a special extension for Comet which lets a servlet handle many requests at the same time on the same thread using event-driven I/O; other servlet containers may offer similar extensions.
There's a new protocol called WebSockets that allows two-way communication over a persistent connection without the overhead of HTTP, but it's not widely supported yet.
Upvotes: 3
Reputation: 3090
There are 4 ways to do this of which I can currently think:
For more details you can check this blog post which also gives some intro into asynchronous support in Servlet 3.0 and Spring MVC 3.2.
Upvotes: 0
Reputation: 1246
When using Comet you can also use the new Asynchonous servlets from servlet api 3.0 - No need for the comet extension for tomcat then.
Upvotes: 0
Reputation: 681
It can be done using either ajax or web sockets.
Using Ajax you can achieve this through long polling which is what most of the big guys are doing now days. Web sockets is the future, but has poor support. If you are running on a node.js environment socket.io is wonderful for this kind of stuff, however, there are options also for ruby, and php.
The beauty of tools like socket.io is that you will be able to use web sockets, which will degrade to ajax long polling where unavailable and therefore keeping your functionality intact.
Upvotes: 1