views:

385

answers:

2

Hi,

I'm trying to establish a HTTP persistent connection from a Silverlight application to a PHP page (ie without creating a new TCP connection for each HTTP request) hosted by an Apache server.

To this end, I need the webserver to send its HTTP responses with the "Connection" header set to "Keep-alive". Client-side, there doesn't seem to be any issue as the network API provided by Silverlight is basically a wrapper of the browser network capabilies, from what I've read : so if the browser supports HTTP 1.1 and Connection: Keep-Alive by default for its requests, it's fine. Content-Length is also well defined, so that the server knows when it has to send the response. However, the server response to the PHP request sets systematically "Connection:" to "close", thus ending the connection and preventing a persistent connection.

I've tried some things to work around this problem : different Methods (GET and POST), explicitly giving a "Connection: keep-alive" to the response with the following PHP code at the beginning of my script :

header("Connection: Keep-alive");

The latter adds the expected header to the response, which is good, but an additionnal "Connection: close" is still appended later in the response headers.

Is it a feature of PHP or Apache which enforces "close" (for some security or performance purpose, I'm guessing) or am I just missing something here ?

Thanks in advance.

P.S. : By sniffing packets, I've noticed that not many websites use "Keep-alive" and the TCP connection is reestablished. Isn't Keepalive the default and preferred behavior under HTTP 1.1 ?

+3  A: 

The Keep-Alive functionality is not meant for persistent connections.

Keep-Alive is meant to reduce the number of connections for a website. Instead of creating a new connection for each image/css/javascript in a webpage many requests will be made re-using the same connection.

There are some settings that prevent this in Apache too, like maximum number of requests on a connection or timeouts between requests. This will also eat your resources very fast because every connection needs its own thread.

You should switch to another solution, that is made for that kind of work.

For services that keep your connection open you can take a look at http://orbited.org and http://twistedmatrix.com/trac/

favo
+1 for not abusing HTTP for persistent connections. It's easy enough to let a PHP daemon listen to a port / socket if needed.
Wrikken
yes, thats also a possibility. in my opionion orbited can do that much easier and is more stable while still providing a basic http server :)
favo
I'm programming a real-time instant messaging app (sort of like Google Wave for the real time aspect) in a restricted context (PHP only and minimal webserver features). I am not even allowed to use datebases. In this kind of pressuring requesting (each keystroke basically yield a request), it would be useful to keep-alive the connection with the server. Thanks for your advice.
ZenithM
+1 for a much better answer than ZZ Coder. Keep-Alives have very little to do with the task described in the comment above by zenithm - there are several approaches, COMET springs to mind although ajax polling is a better option for a scalable system.
symcbean
This answer is surely more elaborate, but I think my problem was just the Apache configuration. Still, thanks for pointing out to me the Comet solution. I'll look into it.
ZenithM
I also have to precise that I have no problem at all with the webserver pushing data to the SL app through a long-lived connection. It's the other way round which causes me trouble.
ZenithM
A: 

Since PHP doesn't manage the HTTP connection, it has no way to change this setting. You need to set that in servers. For example, you can enable keep-alive like this in Apache if you are using mod_php,

KeepAlive On
ZZ Coder
Thanks, this parameter indeed seems to be set to Off on my server.
ZenithM