« Return to Thread: implementing long poll in grails

Re: implementing long poll in grails

by Robert Fischer :: Rate this Message:

Reply to Author | View in Thread

Why is your controller in a loop?  Are you trying for some kind of server-push implementation?

~~ Robert.

redheat wrote:

> Dave,
>
> I'm also implementing long polling. Have you been able to solve your
> original problem?
>
> I'm using plain while with return, not a closure with break. When I tried my
> code with break like you did I didn't have any success.
>
> For your checking of changes, how do you keep track of the last change? I
> used the session context together with beforeInsert in all classes so that a
> Date session.objectcreated gets set when an instance of the class is
> created.
>
> Also I'm still having a couple of things left that I couldn't yet solve.
>
> I have an alarm button on all of my views, so all of them re-open a request
> to the server script.
> When you now switch between views in the browser, firefox aborts running
> requests while IE6 doesn't. So I used req.transport.abort to be safe(using
> prototype) when req is the handler to my xmlhttprequest. This solved the
> problem partially.
> On the server side however the controller with the polling loop continues to
> run until its timeout.
>
> Do you know a good way to recognize that the request was aborted?
>
> A related probem occurs when the user logs out (using acegi). This creates
> an unknown state exception on the server, because the session context is no
> longer there in which I stored my time stamps for the updates. However, I
> can't use a check whether the session context is null because the controller
> is still running with its local instance of the session context so the
> result is not nul, strangely.
>
> I would greatly appreciate if you don't mind sharing some of your
> experience.
>
> Thanks, Martin
>
>
> Dave Crane-3 wrote:
>> Hi,
>>
>> I'm trying to implement a simple long poll in a Grails controller, so that
>>  the controller will go to sleep until something interesting happens, and
>>  maybe take 10-20 seconds before rendering the response. (Yeah, I know
>> this
>>  is a naive, inefficient implementation, and that DWR/Jetty continuations
>>  could do it nicely!) To see whether anything interesting has happened,
>> I'm
>>  sending the latest known version numbers of each domain entity with the
>>  request, and then looking at the domain classes for changes periodically.
>>  So, something like this:
>>
>> def longpoll={
>>   //read known version numbers
>>   def counter = 0
>>   localPoll: while (counter < 10){
>>     def changed=getChanges()
>>     if (changed){
>>       //render a response to say something has changed
>>       break localPoll
>>     }else{
>>       Thread.currentThread.sleep(5000)
>>       counter+=1
>>     }
>>   }
>>   if (counter>10){
>>     //render a null response
>>   }
>> }
>>
>> def getChanges(){
>>   def things=Thing.findAll()
>>   for (t in things){
>>     //is version number different? If so, return true
>>   }
>>   return false
>> }
>>
>> So in the longpoll closure, I enter a loop, and check every 5 seconds for
>> changes, based on incremented version numbers, by iterating through every
>> object in the domain (like I said, I'm not looking for efficiency here!)
>> If I
>> find a change, I report back immediately, and only return a "no change"
>> response after a set timeout, here 50 seconds. My 5 second interval is
>> long,
>> but better for debugging - because I'm just polling on the server, not
>> across
>> the network. I could go for a much smaller interval.
>>
>> Anyway, it's looking like a good pedagogical example of how to do things
>> the
>> hard way, but I find that Thing.findAll() doesn't return the changes in
>> the
>> version number, even if I go in and manually update an entity through a
>> scaffolding/admin screen.
>>
>> I'm guessing that this is because the entire controller closure is wrapped
>> in
>> a single transaction or hibernate session, which is an entirely sensible
>> thing to do when the response returns almost-instantly, as in the majority
>> of
>> web apps. So, my question is, is there a way in which I can manually
>> create a
>> new transaction within the controller closure, so that I can see updates
>> that
>> occur between the request first arriving and the response being rendered?
>>
>> Thanks in advance,
>>
>> Dave
>>
>> -------------------------------------------------------
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>

--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: implementing long poll in grails