SSE (EventSource): why no more than 6 connections?

I wanted to see how many simultaneous SSE (aka EventSource) connections I could establish before rebooting my machine. But testing with Firefox (Firefox 18 or Firefox 20) stopped at 6 connections: additional connections do not give errors, but they do not send any data. (In Firebug, I see them there, waiting for a connection). Chromium 25 also settled on 6 connections, as well as Opera 12.15. But this does not look like a server-side restriction (I use Apache + PHP), since I could run all three browsers (i.e. 18 connections) at the same time, and they all come from the same IP address. (The server and the client are on the same computer, but use the address 172.16.xx, not 127.0.0.1.)

So, I installed the test using CORS and tried to connect to another server with a global IP address. This time I get 12 connections for Firefox. Suggest that this is an Apache configuration in the end? No, Opera still only gets 6 connections. (There is no number for Chrome, since CORS does not work.) I could also start a connection with both servers, a total of 18 connections (but never again) in Firefox and a total of 12 in Opera.

As a third test, I moved both the back-end and html to a remote server and loaded the page in this way. This time I hit a limit of 10 connections for Firefox!?! Opera still has a limit of 6. And Chromium (which works since there is no CORS at that time) has a limit of 6.

I would appreciate understanding where this number 6 comes from, and if it's just a coincidence, then all three browsers are the same. And especially any understanding of why Firefox is sometimes 6, sometimes 10, sometimes 12. (The SSE specification seems to leave the maximum number of connections undefined.)


Apache configuration - using prefork, which means the following settings:

StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 

(Both local (Ubuntu 10.04) and global (Ubuntu 11.10) servers have the same Apache settings.) I believe that the key number of MaxClients is 150. I did a quick experiment by changing StartServers to 50 instead of 5, but getting the same results.


Here is the client HTML / javascript (1 or 2 lines to uncomment and change, if you want to experiment with connecting to another server, as indicated here, it expects to find sse.php in the same directory as HTML):

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>SSE Stresstest</title> </head> <body> <p id="err"></p> <p id="x"></p> <script> function start(){ function onMessage(e){ document.getElementById('x').innerHTML+=e.origin+":"+this.dummy_n+":"+e.data+"<br/>"; }; function onError(e){ document.getElementById('err').innerHTML+="ERR:"+this.dummy_n+":"+JSON.stringify(e)+"<br/>"; }; for(var n=1;n<=32;++n){ //NB. 't' primarily to avoid caching var url='sse.php?dummy_n='+n+'&t='+(new Date().getTime()); //if(n%2==0) // url='http://example.com/sse.php?dummy_n='+n+'&t='+(new Date().getTime()); var es=new EventSource(url); es.dummy_n=n; //So we can identify each one es.addEventListener('error',onError,false); es.addEventListener('message',onMessage,false); } } setTimeout("start()",1000); //Only Safari needs the 1000ms delay. </script> </body> </html> 

And the sse.php script backend looks like this:

 <?php $ip=array_key_exists('SERVER_ADDR',$_SERVER)?$_SERVER['SERVER_ADDR']:'cli'; header('Content-Type: text/event-stream'); header('Access-Control-Allow-Origin: *'); //CORS: allow access from anywhere @ob_flush();@flush(); //Now the main loop while(true){ echo "data:".gmdate("Ymd H:i:s,").$ip."\n\n"; @ob_flush();@flush(); sleep(1); } ?> 
+7
source share
2 answers

The reason may be every EventSource object that initiates a new HTTP session and actually opens a new tcp / ip socket. Due to the fact that you continuously transmit data from the server in an endless loop, the socket continues to work. All web browsers have an upper limit on concurrent active connections to the same server. usually in the range of 4 to 6 according to RFC 2616. Your browser simply prohibits opening a new connection, as this limit will be exceeded.

You can also find out more information here:
http://www.stevesouders.com/blog/2008/03/20/roundup-on-parallel-connections/

+9
source

See the section โ€œConnections for the host nameโ€ here: http://www.browserscope.org/?category=network&v=1

More information than anyone might want, and this shows that the observable โ€œ6โ€ is mostly conditional.

RFC2616 offers a limit of 2, but everyone ignores it. Therefore http://trac.tools.ietf.org/wg/httpbis/trac/ticket/131 removes this sentence.

Customization

It seems IE can be configured from the registry.

Firefox can be configured from approximately: config, filter on network.http for various settings; network.http.max-persistent-connections-per-server is the one that needs to be changed.

Chrome cannot be configured at this time.

Opera can be configured by going to about:config , then open Performance and change the Max Persistent Connections Server.

Safari? No, itโ€™s not configurable, obviously .

+2
source

All Articles