I wrote a script (it may not be perfect, I'm newbie) to check for new message (using ArrowChat and Buddypress) every X mins.

It works good, but I'm worried will it overload my server? It's shared.



// Prepare database
global $wpdb;

// Check are there new messages
$query = $wpdb->get_results( 'SELECT `id` FROM `arrowchat` WHERE `to` = "'.$_GET["user_id"].'" AND `user_read` = "0" AND `read` = "0"');

// Count messages
$x = 0;
foreach( $query as $msg ) {
  $x = $x + 1;
// If there are new messages, echo their number
if(!empty($query)) {
  echo $x;


function checkMessages() {
    type: "GET",
    url: "https://twobytwo.com.hr/check-new-msg.php?user_id='(PHP user id var)'",
    dataType: "html",
    success: function(response) {
        jQuery(".kleo-open-chat > .count").html(response);
setInterval(checkMessages(), 600000);

Thanks in advance.

  • to avoid overload, it's better to not use setInterval but only setTimeout and reschedule the next refresh when you receive the answer of the current refresh. – mmm Dec 1 '17 at 18:30
  • Whether it will overload your server depends mostly on two things: a) the power of your server, and b) the number of users using it at the same time. – janh Dec 1 '17 at 18:44
  • Thank you both for your responses. @mmm , I'm a JS newbie, how could I do that? – Some Guy Dec 2 '17 at 12:53

Thank you for your help. I did it another way. I believe it won't overload in the future when there will be more users.

I will try to explain it, but if anyone will need help with the code I'm willing to help.

In the "send_message.php" file, after database insert, I added a piece of code that creates a new empty text file with receiving user id as a name.

In the "receive_user.php" file, after database update (message read), I also added a piece of code, but this one deletes that same text file from above.

Finally, the PHP code used by AJAX checks if text file with logged in user id as filename exists. If it does, it echoes 1.

As you can see, there's no constant database checking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.