Should I expect Comet to be this slow?

Posted by Chad Johnson on Stack Overflow See other posts from Stack Overflow or by Chad Johnson
Published on 2010-04-19T18:29:27Z Indexed on 2010/04/19 18:33 UTC
Read the original article Hit count: 304

Filed under:
|

I have the following in a Rails controller:

def poll
  records = []
  start_time = Time.now.to_i

  while records.length == 0 do
    records = Something.uncached{Something.find(:all,
        :conditions => { :some_condition => false})}

    if records.length > 0
      break
    end

    sleep 1

    if Time.now.to_i - start_time >= 20
      break
    end
  end

  responseData = []

  records.each do |record|
    responseData << {
      'something' => record.some_value
    }

    # Flag message as received.
    record.some_condition = true
    record.save
  end

  render :text => responseData.to_json
end

and then I have Javascript performing an AJAX request. The request sits there for 20 seconds or until the controller method finds a record in the database, waiting. That works.

function poll() {
  $.ajax({
    url: '/my_controller/poll',
    type: 'GET',
    dataType: 'json',
    cache: false,
    data: 'time=' + new Date().getTime(),
    success: function(response) {
      // show response here
    },
    complete: function() {
      poll();
    },
    error: function() {
      alert('error');
      poll();
    }
  });
}

When I have 5 - 10 tabs open in my browser, my web application becomes super slow.

Is this to be expected? Or is there some obvious improvement(s) I can make?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about comet