« Return to Thread: implementing long poll in grails

implementing long poll in grails

by Dave Crane-3 :: Rate this Message:

Reply to Author | View in Thread

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


 « Return to Thread: implementing long poll in grails