1

I was implementing the server-push content using comet to the client browser. There should be updates when mysql got insert then something will happen to client, so I'm currently doing by PHP like this:

PHP
  while (check database if update is there)
  {
    usleep(10000); 
   // do write json
  }

It actually working, but what happening to me is my hosting(shared) run out of resource even when I just test with 5 simultaneous clients. CPU is 40% loading at this moment and cause account disable.

So please someone advise any idea how to trigger php to send out the new update only without looping check.

3 Answers 3

2

I think your problem is that you have a slight error regarding usleep. The parameter should be:

micro seconds
Halt time in micro seconds. A micro second is one millionth of a second.

Your script currently halts for 0.01 seconds instead of 10 seconds, which, I assume, is not what you wanted.


use:

usleep(10 * 1000000);

or:

sleep(10);
Sign up to request clarification or add additional context in comments.

Comments

2

I don't know how your Javascript looks like, but I think you're approaching it incorrect. What you really wanna do is probably to send requests from the browser with 10 secs intervals. See this example.

Comments

1

This method will not use any CPU during the sleep time:

set_time_limit(0);
while (check database if update is there) {
   sleep(10); 
   // do write json
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.