Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:00:41+00:00 2026-06-14T12:00:41+00:00

I’ve made a simple PHP jQuery Chat Application with Short Polling (AJAX Refresh). Like,

  • 0

I’ve made a simple PHP jQuery Chat Application with Short Polling (AJAX Refresh). Like, every 2 – 3 seconds it asks for new messages. But, I read that Long Polling is a better approach for Chat applications. So, I went through some Long Polling scripts.
I made like this:

Javascript:

$("#submit").click(function(){
    $.ajax({
        url: 'chat-handler.php',
        dataType: 'json',
        data: {action : 'read', message : 'message'}
    });
});

var getNewMessage = function() {
    $.ajax({
        url: 'chat-handler.php',
        dataType: 'json',
        data: {action : 'read', message : 'message'},
        function(data){
            alert(data);
        }
    });

    getNewMessage();
}

$(document).ready(getNewMessage);

PHP

<?php
    $time = time();
    while ((time() - $time) < 25) {
        $data = $db->getNewMessage ();

        if (!empty ($data)) {
            echo json_encode ($data);
            break;
        }

        usleep(1000000); // 1 Second
    }
?>

The problem is, once getNewMessage() starts, it executes unless it gets some response (from chat-handler.php). It executes recursively. But if someone wants to send a message in between, then actually that function ($("#submit").click()) never executes as getNewMessage() is still executing. So is there any workaround?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:00:43+00:00Added an answer on June 14, 2026 at 12:00 pm

    I strongly recommend that you read up on two things: the idea behind long polling, and jQuery callbacks. I’ll quickly go into both, but only in as much detail as this box allows me to.

    Long polling

    The idea behind long polling is to have the webserver artificially “slow down” when returning the request so that it waits until an event has come up, and then immediately gives the information, and closes the connection. This means that your server will be sitting idle for a while (well, not idle, but you know what I mean), until it finally gets the info that a message went through, sends that back to the client, and proceeds to the next one.

    On the JS client side, the effect is that the Ajax callback (this is the important bit) is delayed.

    jQuery .ajax()

    $.ajax() returns immediately. This is not good. You have two choices to remedy this:

    • bind your recursion call in the success and error callback functions (this is important. the error function might very well come up due to a timeout)
    • (see below):

    Use This:

    var x = $.ajax({blah});
    $.when(x).done(function(a) { recursiveCallHere(); });
    

    Both amount to the same thing in the end. You’re triggering your recursion on callback and not on initiation.

    P.S: what’s wrong with sleep(1)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.