4

I want to build a chat, based on JavaScript (jQuery will be used for AJAX) and PHP.

I've heard a good way of doing this is to use long-polling.

I do understand the idea, but I don't know how to implement it on the server side.

An infinite loop sounds like a bad idea.

3
  • 1
    Using PHP for that is not a good idea. Have you considered using something like nodejs? Commented Oct 16, 2012 at 16:19
  • 1
    Agree that something non-blocking like node.js would probably be more ideal, but if that isn't an option you can implement in PHP as in my answer below. Commented Oct 16, 2012 at 16:20
  • Have you tried WebSockets instead? Commented Oct 16, 2012 at 16:28

2 Answers 2

7

You don't want to create an infinite loop, but you can set a timeout. Basically loop for X second waiting for some sort of data, and if that doesn't happen send a response to the client telling it that it needs to initiate a new request, which will have the same timeout period.

$source; // some data source - db, etc
$data = null; // our return data
$timeout = 30; // timeout in seconds
$now = time(); // start time

// loop for $timeout seconds from $now until we get $data
while((time() - $now) < $timeout) {
    // fetch $data
    $data = $source->getData();

    // if we got $data, break the loop
    if (!empty($data)) break;

    // wait 1 sec to check for new $data
    usleep(10000);
}

// if there is no $data, tell the client to re-request (arbitrary status message)
if (empty($data)) $data = array('status'=>'no-data');

// send $data response to client
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

1 Comment

why isn't processor's usage 100% 1) SLEEP inside the loop - processor runs the loop just once and then puts the script aside for a while and then comes back to it = no 100% processor usage 2) END OF LOOP case A: ((time() - $now) < $timeout) = ((CURRENT_TIME - TIME_WHEN_THE_SCRIPT_STARTED) < TIMEOUT_SET_BY_YOU) CURRENT_TIME - TIME_WHEN_THE_SCRIPT_STARTED = CURRENT_TIMEOUT so when CURRENT_TIMEOUT > TIMEOUT_SET_BY_YOU the loop will end 3) END OF LOOP case B: if (!empty($data)) break; = If the loop succeeds in getting the data it breaks=>ends
0

implementing such kind of chat is not good idea in php, you can use CometChat, Nodjs and if you can not install scripts on your server than you can use Available APIs for Realtime Sending Data,

such as Pubnub, Pushemr, Beaconpush.

2 Comments

Why is using PHP not a good idea, can you give more details on your reasoning?
Because PHP is not made for this... PHP can't handle large number of simultaneous connections .. and will be much overhead on php server.. so right technology for right requirement .. "A Sword cannot replace a Needle"

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.