Moon
Moon

Reputation: 22565

Does Ajax connection disconnect at some point?

My Co-worker told me that AJAX connection alive until a user closes his/her browser. As far as I know, ajax connection closes its connection when its request has completed. I tested it with Firebug and a HTTP monitoring tool, I noticed that AJAX connection closes itself.

Is he correct????

Upvotes: 1

Views: 6459

Answers (5)

therightstuff
therightstuff

Reputation: 1027

Using JQuery to make AJAX requests, some connections are maintained until the following AJAX call. This can become a real problem when the server holds streams open until the response's close event fires.

Upvotes: 0

Mike DeSimone
Mike DeSimone

Reputation: 42805

Late to the party here, but this regards an issue I'm actually dealing with right now...

I have a web server running on a severely resource constrained platform (128k Flash, 48k RAM) so it can only handle one connection at a time. Further connections are not handled until the current one is closed. Also, it does not force Connection: close on certain URLs because of a low latency requirement. In general, only one thing talks to the device at a time.

AJAX connections follow whatever rules the browser sets for other connections. In my case, I'm testing a web page that uses AJAX to read one of the keep-alive-allowed URLs once per second, and the browser does not close the connection until several seconds after the window is closed. As a result, other clients wait indefinitely.

So don't assume that XHR connections are closed when complete. They might not be; Firefox 21 sure isn't closing them.

My current problem is that I want to force my AJAX requests to close the socket on completion, and I'm using jQuery's .ajaxSend() pre-send hook to set the Connection: close header. The AJAX seems to be working, but when another client tries to connect, it gets "connection reset by peer", so I'm wondering if Firefox doesn't notice the "Connection: close" header on the XHR request and keeps its end of the socket open (until it times out after approximately three seconds) even after the server has closed its side.

Upvotes: 0

Sergey Vedernikov
Sergey Vedernikov

Reputation: 7754

Ajax connections closed after receiving data or if you close tab, then connections will force closed.

Here described Ajax life cycle.

Upvotes: 1

p.campbell
p.campbell

Reputation: 100577

Break down what AJAX is -- and XMLHttpRequest. It's a connection to a URI endpoint for some resource (image, text, whatever). Your browser closes the HTTP connection as soon as it's done.

Upvotes: 1

JohnP
JohnP

Reputation: 50019

Ajax is just like any other request, when it completes the connection is closed. Your colleague is wrong.

Note : There are connection types that allow you to keep the connection open indefinitely

Upvotes: 3

Related Questions