0

I'm using comet polling in Joomla (PHP) application on shared hosting. I can't use either WebSockets or Node.js, as they might not be available on client's server.

I managed to maintain only one request throughout to reduce the space it will take up in Apache. However it still makes my PC very very slow. In my friend's MacBook even MySQL crashed when my app was running.

I know that PHP and Apache are not the right choice for real-time stuff but I've no choice.

How to use Comet polling without taking up Apache space?

The while loop checks for new data constantly and only return to client when there's new data.

while ($currentmodif == $lastmodif) {           
    // sleep 10ms to unload the CPU
    usleep(10000); 
    clearstatcache();
    $currentmodif = $model->totalNoficationComet($userId);//checks new data
}
Arseni Mourzenko
  • 134,780
  • 31
  • 343
  • 513
112233
  • 147
  • 1
  • 5
  • 2
    *"I know that PHP and Apache are not the right choice for real-time stuff"*: how do you know that? – Arseni Mourzenko Sep 15 '16 at 13:47
  • 6
    @MainMa (S)He hasn't answered, but (s)he's not wrong. Even with modern technologies like Ratchet PHP is simply not made for bi-directional communication which requires the server to run in an event loop. PHP will eventually (and unfortunately much sooner than let's say Node) run out of memory, because its GC does not know whether variables may be invalidated or not just yet. [PHP is meant to die](https://software-gunslinger.tumblr.com/post/47131406821/php-is-meant-to-die). – Andy Sep 15 '16 at 14:30

2 Answers2

1

The key is to use a timer on the client side. For example:

To receive the chat messages of all users from the database table, a timer function is set for 5 seconds using the following JavaScript command. This will execute get_chat_msg function in 5 seconds interval.

var t = setInterval(function(){get_chat_msg()},5000);

The get_chat_msg is an Ajax based function. It executes chat_recv_ajax.php program to get chat messages from the database table. In the onreadystatechange property, another JavaScript function get_chat_msg_result is connected. While getting back the chat messages from database table, the program control goes to the get_chat_msg_result function.

References

Paul Sweatte
  • 382
  • 2
  • 15
0

I'll talk about the hardware part, okay? You could use nginx with apache ... But this is a shared hosting. What I see, in this case, is switch to a VPS. I recommend Google Cloud, which has 1 year free. With that time, you can test and migrate to another server that is cheap, but I recommend staying with a VPS. With this little modification, you will have more authority over space and memory. Send news, about your solution!