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){ </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); } ?>