Rails : fighting long http response times with ajax. Is it a good idea? Please, help with implementa

Posted by baranov on Stack Overflow See other posts from Stack Overflow or by baranov
Published on 2010-03-31T11:04:16Z Indexed on 2010/04/01 2:33 UTC
Read the original article Hit count: 312

Hi, everybody!

I've googled some tutorials, browsed some SO answers, and was unable to find a recipe for my problem.

I'm writing a web site which is supposed to display almost realtime stock chart. Data is stored in constantly updating MySQL database, I wrote a find_by_sql query code which fetches all the data I need to get my chart drawn. Everything is ok, except performance - it takes from one second to one minute for different queries to fetch all the data from the database, this time includes necessary (My)SQL-server side calculations. This is simply unacceptable.

I got the following idea: if the data is queried from the MySQL server one point a time instead of entire dataset, it takes only about 1-100ms to get an individual point.

I imagine the data fetch process might be browser-driven. After the user presses the button in order to get a chart drawn, controller makes one request to the database and renders, say, a progress bar, say 1% ready. When the browser gets the response, it immediately makes an (ajax) request, and the server fetches the next piece of data and renders "2%".

And so on, until all the data is ready and the server displays the requested chart. Could this be implemented in rails+js, is there a tutorial for solving a similar problem on the Web? I suppose if the thing is feasible at all, somebody should have already done this before.

I have read several articles about ajax, I believe I do understand general principles, but never did nontrivial ajax programming myself.

Thanks for your time!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about AJAX