Agum Posted May 22, 2007 Share Posted May 22, 2007 I'm working on an interesting web app and here's a situation I'm going to get to: * User performs an action, which starts this "session"... * During this "session", user will be sending requests (performing actions) to the server every 3 secs, or 5 secs. These are AJAX (xmlHttpRequest) requests. * This could go on for 5~10 times before this "session" ends. (when I say "session", it has nothing to do with cookies, I just mean the word "session" in literal meaning) Now, due to the information needed to perform every action, each request would need to do a MySQL query, grab some information, then do some calculation, and write to the db. If we consider possibly large user traffic, then I'd want to do this as efficiently as possible. I can do this the normal way -- have xmlHttpRequest send to a php script, then it will go through and do all that needs to be done. (script begins -> SQL read -> processing code -> SQL write -> output to client -> script ends) Is there any ways I could make this more efficient? What I don't know is if it's possible to make it only one script execution, while it waits for new user actions. Effectively, this means: script begins -> SQL read -> processing code -> SQL write -> output to client -> wait for next event -> processing code, SQL write, output and repeat until last action -> script ends This saves 1 MySQL query for every action done, because only one read initially is needed for the entire lifetime in my specific case. However, how can this be done at all? The PHP script and the JS would need to have some kind of interaction, more than just the xmlHttpRequest. (because if a request was made, then it's starting a new php script execution) I thought about using cookies, having JS set some cookies for each action done, and have the php script read it every 5 secs -- but that creates sync'ing problem and I don't think JS can set cookies before the whole page refreshes, anyway. (was that totally confusing?) Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted May 22, 2007 Share Posted May 22, 2007 instead of running the ajax at set times simply cal it when the user does do something that warrants a databse update (this should increase time between requests to every 30 seconds or maybe more!) You may want to consider using persistent connections - but beware they have limits on numbers so you'll just have to figure that one yourself. Otherwise it all sounds fine... make sure your php code is as efficient as possible and maybe consider caching some results... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.