Wolphie Posted May 19, 2009 Share Posted May 19, 2009 Is it possible to intentionally hang an AJAX application to listen for any incoming data? For example, you have a chat room... you open a connection to get any new messages... if they're are no new messages then keep the connection open until a message is available. But have a timeout interval so it doesn't cause any over head? Correct me if I'm wrong about anything, it's just I'm creating a chat application and would like to know the most efficient and viable method of retrieving messages, the user list etc.. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Well with an AJAX chat system, you can't "hang" the connection. You can use setInterval() which sets off a call to an AJAX function every 3 seconds or so. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted May 19, 2009 Author Share Posted May 19, 2009 No, but couldn't you use sleep in PHP to 'hang' the connection intentionally? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Yeah, but so what? You would just delay the execution of the rest of the script. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted May 19, 2009 Author Share Posted May 19, 2009 The rest of the script wouldn't necessarily need to be executed. You're only needed to listen for any new messages that have been sent and retrieve them. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Read up on AJAX. What it does is JavaScript sends a request to a PHP file and waits for it to send back some response. If the PHP script hangs itself, so does JavaScript. So nothing will happen. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.