Changing html <-> ajax <-> php/mysql to threaded approach

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-03-30T00:02:19Z Indexed on 2010/03/30 0:03 UTC
Read the original article Hit count: 408

Filed under:
|
|
|
|

I have an application that needs to be updated real-time. There are various counters and other information that have to come from the database and the system needs to be up to date for the user.

My approach now is just a normal ajax request every second to get the new values from the database. There is a JavaScript which loops every second getting the values trough ajax. This works fine but I think its very inefficient.

The problem

  • There is an ajax script that loops every second requesting data from php
  • # On the server it has to load the PHP interpeter
  • The PHP file has to get the data and format it correctly
  • # PHP has to make a connection with the mysql database
  • Work with the database(reads,never writes)
  • Format the data so it can be send
  • Send the data back to the browser
  • # Close the database connection, and close the php interpeter
  • Last the browser has to read these values and update the various html parts

Now with this approach it has to load the interpreter and make a db connection every second. I was thinking of a way to make this more efficient, and maybe use a threaded approach to this.

Threaded aprouch

  • Do a post to the PHP when you enter the page and keep the connection alive
  • In PHP only load the interpreter once, and make a connection to the DB ones
  • Every second send an ajax response to the javascript listener
  • The javascript listener than just changes values as the response from php arrives.

I think this approach will be a great optimization to the server load and overall performance. But I can spot some weak point in the system and i need some help with these.

Problems with the approach

PHP execution time limit
I don't think PHP is designed for such a setup. I know there is a time limit on php script execution. I don't know if an everlasting loop in PHP will cause any serious cpu/memory problems.

Sending ajax request without breaking
I don't know if it is possible to have just one ajax post action and have open and accepting data.

user exists the page
What will happen when the user exists the page and the PHP script is still going. Will it go on forever.

security issues so far i can't think of any security issues. Almost every setup you use have some security issues. Maybe there are some with this solution I do not know of.

Open to other solution
I really want to change the setup as it is now and move to a threaded approach or better. If someone has a better approach to tackle this I definitely want to hear that.

Maybe the usage of some other scripts is better suited for having an ongoing runtime. I only know php and java so any suggestions are welcome and I am willing to dig trough. I know there are things like perl, python etcetera that are used for this type of threaded but i don't know which one is best suited.

When using other script If the best way is to go with other type of script like perl,python etcetera I do have some critera.

  • The script has to be accessible via ajax post
    • If it accepts some kind of json encode/decode it would be nice
  • The script has to be able to access the session file
    • This is essential because I need to know if the user is logged in
  • The script has to be able to easily talk to MySQL

All comments are welcome, and I hope this question is helpful to other also.

Cheers!

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX