1

I have developed a chat site which uses reverse-ajax/COMET with php backend and works pretty well, but when I run a page in two parallel tabs of the same browser, the response slows down because the 2nd tab waits for the 1st tab to finish its execution and then the 1st tab waits for the 2nd. As a result, the site really slows down.

So PHP doesn't support simultaneous execution. How do I fix this problem?

1 Answers1

0

You must understand that PHP is server-side and Javascript is client-side. Your browser runs on client-side, which theoretically is running on computer A, while the PHP code, which is server-side code runs on computer B. As a result, it should not matter whether you are opening n tabs on the same browser to run the page or you are opening n different browsers. PHP should run without problems, two tabs should not really affect scalability. I believe you have one or more bugs in your code which leads to this problem.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • thank you for the clear explanation... Ok , I may not be a php master like you but what kinda bug there could be on the client-side that effects another tab? When I run the site in two different browsers it runs well, recently I found out something called pthread which seems to be the solution for this kind of problem but I need to me sure about alternatives. can you help me to diagnose the bug? my site is online, i'd be glad if you can. –  Mar 16 '15 at 14:26
  • I am happy to help. It is important to clarify that your client-side is the Javascript and CSS. Javascript is not multithreaded, so, if your problem lies in the client-side, then you probably have a problem in your Javascript code. Also, if the HTML you generate is too large, it could cause problems. I believe you should edit your question and add some code, so we can have more information about the problem. Please, add only the relevant part of the code. – Lajos Arpad Mar 16 '15 at 17:36
  • I've added notification system, please check. –  Mar 16 '15 at 18:27
  • Nice. Can you open this in two browser tabs and debug the Javascript code in one of the tabs? (https://developer.chrome.com/devtools/docs/javascript-debugging) – Lajos Arpad Mar 16 '15 at 18:57
  • I could not detect the problem, I tried to upload the results but I need to have 10 reps to upload images.... what are you suggestion? should provide the url for you to check? –  Mar 16 '15 at 19:56
  • That could be helpful, but here we want to teach you the approach with which you can solve the problems yourself. If debugging did not yield the results for you, then maybe benchmarking will help. Try this out: https://developer.chrome.com/devtools/docs/cpu-profiling (on a final note, I can check your url, but then both my explaining and your information absorbing has failed. We want to achieve victory) – Lajos Arpad Mar 16 '15 at 21:15
  • I have detected the problem!! thank you very much, you set me to the right path, I really appreciate your help :) –  Mar 16 '15 at 21:16
  • I am happy I could help. As I can see, you have a deadlock problem. You need to make sure you do not have unnecessary dependencies. A timeout is also a useful addition. Finally, you can make prepared chunks on the server. If you always have the timestamp of the last event, then you can sent the timestamp you last refreshed from the client-side and the server would check if there was newer event. These might be far-fetched ideas right now. You are improving, but if you find some ideas difficult now, do not worry, later they will be simple. Just practice. – Lajos Arpad Mar 16 '15 at 23:32
  • hi, I still need your help, thanks to you I found out that the problem causes from js, here is what i found. I am running 3 parallel comets, when i run only 2 of them the site seems to be running well, but I have a chat room in my site which additionally runs 2 more comets so totally 5 comets are running in parallel, I have been debugging but could not find anything that I can diagnose the problem.. the url is www.ovlee.com, login is required here is a test account, (username = testuser ) and (password= testuser ) I really need your help (( –  Mar 17 '15 at 11:59
  • My first observation is that you intend to call setFriends at the end of the script, however, the function does not exist. Also, you are polling update_me.php, where you send myid with value, but do not receive a response. – Lajos Arpad Mar 17 '15 at 12:39
  • I double checked setFriends and update_me.php, upload_me doesn't suppose to send response back.... if you noticed it all works pretty good with one tab... do you think it's deadlock? –  Mar 17 '15 at 15:17
  • In any case, it is a synchronization problem. It can be a deadlock problem, but I believe a concurrency problem is more probable. Can you explain the algorithm, how did you realize the refreshing? If you tell me the idea, we might find the problem. At least I hope so. – Lajos Arpad Mar 18 '15 at 08:02
  • I made everything pretty much like Facebook, i have notification system which notifies users about friend requests and friend approvals and a friend activity system which changes friend's status (online/offline) and a private message system.. all of the run based on engine function which is ajax... but now I realised that comet actually work pretty well... I opened 3 tabs with same account and send a message to it from another account with another browser it works .. but all the rest even page it self slows down, reloading etc .. I could not find any information regarding to this problem. :( –  Mar 18 '15 at 22:33
  • Ok, in this type of case I have a technique, an approach. I comment out almost all the code and see if in that case I can reproduce the problem. Probably this way, the problem is reproduced. Then I uncomment a part and see if I still can reproduce. This way, each time you uncomment a part, you are narrowing down the problem. If the problem is still reproducing, then it is inside the commented code. If not, then it is in the uncommented part. If you repeatedly narrow the problem-space until you find the problem, then you are on the right track. – Lajos Arpad Mar 19 '15 at 10:22
  • Here is what I did, I wrote a new index file that runs 3 comets at the same time, (please check out the original question for codes) again, with 1 tab it works after oppening 3 taps it slows down again... i debugged it but nothing was found.. I also posted backend file, please check. –  Mar 19 '15 at 21:34
  • I believe you have a problem in your backend file. Are you sure you need to call usleep for all the notifications? usleep sounds like a sub-optimal idea. Imagine, if you have 6 new notifications to send, then you wait for 200000 * 6 microseconds, which is 1 200 000 microseconds, which is more than a second. If you are polling from 3 tabs, then all 3 tabs will work out all the notifications, due to the fact that the requests will surely reach the server in less than 200 000 microseconds. Use polling from the client-side, or push notifications. – Lajos Arpad Mar 20 '15 at 08:43