By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

persistent ajax/comet

rohypnol
P: 54
Hi!

Is there a way to have a persistent connection between the client and the server, through which to pass data from one to the other? For example, having a webchat application that connects to the server using the XMLHttpRequest javascript object and keeps the connection alive until the page is closed (consider a "perfect" environment where it's not interrupted by 3rd party) or until the client decides to disconnect. How would the client pass data to the server and viceversa?

The best method I know for this is to have a connection which is reopened after the client receives information, in order to use the POST method to pass a response.

After reading the first paragraph, I guess my question would be: can the client respond to information pulled from the server using the same XMLHttpRequest object?

I'm interested in both the PHP and JavaScript, if this is possible.

Regards,
Tom
Dec 27 '07 #1
Share this Question
Share on Google+
1 Reply


pbmods
Expert 5K+
P: 5,821
Heya, Tom.

This is not possible. HTTP is stateless by nature; as soon as the request is fulfilled, the connection is terminated.

In the situation you are describing, you can simply create a new instance of an XMLHttpRequest object and send() it as soon as the current XMLHttpRequest instance's onreadystatechange() fires.

It doesn't matter which instance you use; it's a new request as far as the server is concerned.

The browser will pass a session ID with the request, so you can use $_SESSION to store User-specific information.
Jan 2 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.