This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Long Polling

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm able to poll the server by calling a servlet at regular intervals to see if new data has come in from other clients.

But this is a little inefficient, and theres always a delay for the client.
Is it possible to poll a servlet once, and use a timer to test for new data, so it can then return information to the client?

I'm not sure how to do this, cause even if I start a timer in a servlet, the servlet code will just complete and the response is returned.
So I want to know if I can delay this using timers.

I'm not able to use comet either on my web hosting, so I'm looking at alternatives... thanks for any help.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course not. What if the client is off checking Facebook or watching YouTube videos? HTTP works on a request/response cycle and you have to live within it unless you want to explore alternatives such as privileged fat clients.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks, but suppose you had a normal J2SE program on client machine.
This sends data to a servlet... the servlet then runs the code below:

Then clearly its bad as it will be very cpu intensive, and will probably cause problems.
But other than that, I don't see the problem. But I don't think there is any simple solution
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
colin shuker wrote:I'm able to poll the server by calling a servlet at regular intervals to see if new data has come in from other clients.


This is the simple solution you didn't think existed.

You don't think it's a problem that the client's browser is hung while your servlet is busy-waiting? (Busy-waiting is also a problem.) HTTP requests should be responded to as quickly as possible. If they take more than a few seconds then they are too slow.

With the simple solution the client doesn't have to sit there with the cursor spinning in their browser, they can go and do other things. They can come back, even the next day, and connect to the servlet to see if the data is ready yet.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks, but how often is question...

For example, if I had a chat application, and the server was polled once every 5 seconds, it would be annoying to have to keep waiting.

But would say 5 times per second be too much?
It sounds a lot, but maybe not, as far as the cpu is concerned, 200 milliseconds is quite a long time.
Any thoughts?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic