jamesotron
jamesotron

Reputation: 65

Long polling from Rails 3.2

I have a carrierwave upload that takes a lot of time to process, but is required to finish before the user can continue on to the next action in the browser. I'm using an Ajax file upload on the front-end, so the app UI gives progress updates on the upload and processing. This works fine on my dev environment because the timeout on my dev server is relatively long, however not so good on Heroku since Cedar times out the request after 30 seconds if no response is sent. I've been trying to create a streaming response which sends a space every couple of seconds until the process has completed by creating a response object which responds to each thus:

class LongPoller
  def initialize(yield_every=2,task)
    @yield_every = yield_every
    @task = task
  end

  def each
    t = Thread.new(&@task)
    while t.alive?
      sleep @yield_every
      yield ' '
    end
    yield t.value.to_json
  end
end

This isn't working as expected though, because Thin seems to be batching the responses and not sending them back to the client.

Anyone have any ideas how I can get this to work?

Upvotes: 3

Views: 854

Answers (0)

Related Questions