Comet style requests with mongrel

Hi,

I’ve been doing some experiments with php and if can use something like

<?php for ($i = 0; $i < 100; $i++) { echo "hello world"; flush(); sleep(10); } ?>

to keep a http link open for an arbitrary amount of time and
routinely display “hello world” in the browser window.

Is is possible to write a mongrel handler to do something similar?

Thanks

jeb

On Fri, 23 Nov 2007 16:26:04 +0000
Jeremy W. [email protected] wrote:

}

?>

to keep a http link open for an arbitrary amount of time and
routinely display “hello world” in the browser window.

Is is possible to write a mongrel handler to do something similar?

Yep, you could probably write a handler to do that, but probably not a
rails action without tons of surgery.

However, this will fall down pretty quick once you hit Ruby’s 1024 (or
less) file descriptor limit due to its use of select.

Zed

You might find this interesting.

http://adam.blogs.bitscribe.net/2007/05/08/comet-with-rails-mongrel/
Google Code Archive - Long-term storage for Google Code Project Hosting.

-Todd

Cheers for the reply, the intention was to use a custom handler to
bypass rails for just this one action. I’m currently using a poll,
and am looking into the possibility of keeping the connection open
instead. There will only be a maximum of 15 connections open at any
one time - would this still be limited by ruby file descriptor limit?

I’ve managed to get mongrel handler almost doing what I want but not
quite - I can’t tell mongrel to flush data down the socket at the
moment, is there a way to go about this. I’ve had a look at

http://adam.blogs.bitscribe.net/2007/05/08/comet-with-rails-mongrel/

but can’t get this approach to work since it seems to depend on a non
existant HTTPResponse.flush method.

Thanks

Jeremy

On Mon, 26 Nov 2007 09:30:29 +0000
Jeremy W. [email protected] wrote:

Cheers for the reply, the intention was to use a custom handler to
bypass rails for just this one action. I’m currently using a poll,
and am looking into the possibility of keeping the connection open
instead. There will only be a maximum of 15 connections open at any
one time - would this still be limited by ruby file descriptor limit?

What Eden said, additionally, your sample in the blog post will mostly
work, but be careful of threading issues. In Ruby it’s not so bad, but
in JRuby you can get hit with weird bugs because they use real threads.

For example, double check that the connection object you’re using to
query the SQL server doesn’t need a little lock.


Zed A. Shaw

Try doing the sleep in the response.start {|head, out| ... } block,
and call #flush on out.