# AJAX : Server to Client callback possible?

**URL:** <https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996>\
**Category:** Rails\
**Created:** [December 23, 2005, 5:47pm UTC](https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996 "2005-12-23T17:47:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![olivier\_Hericord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@olivier\_Hericord](https://www.ruby-forum.com/u/olivier_Hericord)\
**Post date:** [December 23, 2005, 5:47pm UTC](https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996/1 "2005-12-23T17:47:06Z")

</div>

hi,

is there a way (with rails) to have live updates triggerred by the  
server on the client side?  
is there a little javascript lib i didn’t know that can listen to  
incoming server orders…

a friend of mine thought about an hidden .swf to make the socket  
connection and that can launch javascript instructions into the client  
web page.

any working solutions to point me to ?

thanks

---

<div class="post-metadata">

**Author:** ![olivier\_Hericord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@olivier\_Hericord](https://www.ruby-forum.com/u/olivier_Hericord)\
**Post date:** [December 23, 2005, 6:14pm UTC](https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996/2 "2005-12-23T18:14:21Z")

</div>

There is no interrupt model for Javascript – you’re stuck with polling  
(timed XmlHttpRequests), which is far from efficient or elegant.

A SWF opening a socket is how I would handle this sort of scenario. Your  
onData (etc) callback in the SWF can call JS as needed.

---

<div class="post-metadata">

**Author:** ![olivier\_Hericord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@olivier\_Hericord](https://www.ruby-forum.com/u/olivier_Hericord)\
**Post date:** [December 23, 2005, 7:29pm UTC](https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996/3 "2005-12-23T19:29:52Z")

</div>

In Java land I use something called ‘pushlets’ to achieve this - it  
basically opens up a connection to the web server and keeps the  
connection  
open so that the server can stream info back up to the client. Works  
great.  
Javascript on the client manages the connection, the XML content etc.

The project is opensource, so make a name for yourself and convert it or  
jruby it or something 🙂

Good luck,  
Geoff

---

<div class="post-metadata">

**Author:** ![olivier\_Hericord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@olivier\_Hericord](https://www.ruby-forum.com/u/olivier_Hericord)\
**Post date:** [December 23, 2005, 7:48pm UTC](https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996/4 "2005-12-23T19:48:26Z")

</div>

Just a thought, but couldn’t you make an AJAX call back to the server,  
and the server, instead of replying right away, waits until an event  
happens and then replies to the AJAX request? This gets the “open  
connection waiting for an event” and lets you stay in ruby/rails  
(assuming you can wait on the reply like that without adverse effects on  
the server).

I think for this to work you’d have to unhook the AJAX call from being  
tied to a particular button/link (like how rails does it), and instead  
make the AJAX call as part of the page onLoad handler, so it could be  
listening in the background. You’d also need a way to start a new AJAX  
request after you received each event.

Sounds like it could work …

---

<div class="post-metadata">

**Author:** ![olivier\_Hericord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@olivier\_Hericord](https://www.ruby-forum.com/u/olivier_Hericord)\
**Post date:** [December 23, 2005, 8:12pm UTC](https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996/5 "2005-12-23T20:12:33Z")

</div>

On 12/23/05, Jeff de Vries [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just a thought, but couldn’t you make an AJAX call back to the server,  
> and the server, instead of replying right away, waits until an event  
> happens and then replies to the AJAX request? This gets the “open  
> connection waiting for an event” and lets you stay in ruby/rails  
> (assuming you can wait on the reply like that without adverse effects on  
> the server).

I thought about this, but you might face a browser or server timeout  
error.

---

<div class="post-metadata">

**Author:** ![olivier\_Hericord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c68b51/32.png) [@olivier\_Hericord](https://www.ruby-forum.com/u/olivier_Hericord)\
**Post date:** [December 23, 2005, 8:24pm UTC](https://www.ruby-forum.com/t/ajax-server-to-client-callback-possible/50996/6 "2005-12-23T20:24:37Z")

</div>

Josh C. wrote:

> > 
> 
> I thought about this, but you might face a browser or server timeout error.

what about using periodically\_call\_remote? call the server every 30  
seconds or so. the server would only hold the connection open for 29.5  
seconds. The client would reestablish the connection every x seconds.

call
