Pregunta

I'm working on a project that requires endless ajax requests. The request is stop by the server when there are data to send.

To simplify the test, I made a sample to simulate the problem :

<?php
// Close (and disable) session to avoid lock
session_start();
session_write_close();

echo '<pre>';

do {
    echo time() . "\n"; ob_flush(); flush();
    error_log($_GET['id'] . ' - ' . time());
    usleep(2000000); // sleep 2s to unload the CPU

} while (1);

On Chrome, loading this page will display every two seconds the new timestamp. On firefox, nothing will be displayed, that why I put an error_log.

If I open the script 6 times, all is ok. But if I open it one more time, the new tab displays nothing and waits that one process stops. With private mode or an other browser, I can call the script 6* times.

It seems that Apache or PHP limit to 6 connections per client/session. Any idea?

Edit:

It seems, that's a limitation of the browser: https://stackoverflow.com/a/985704/3036602 Anyone know how to avoid this limitation?

Edit 2: After searching on the web, I see that I'm not the only one with this problem. I did not find any easy solution. I found two : - Facebook approach: using different subdomain per tab. This solution asks to have a specific web server and DNS. - Local storage: Only one tab allowed to make requests. This solution is not so bad, but need some works - Local storage: forbidden multiple tabs (easiest solution, but not really sexy)

¿Fue útil?

Solución

I finally found a solution.

Browser do not allow too many connections to a domain at the same time. For firefox and chrome, the limit is 6.

One of the solution, is to use subdomain. To do that, you need to add an entry for *.mydomain.tld on the DNS. On the PHP script you need to add

header('Access-Control-Allow-Origin: *');

to allow requests from others domains.

And finaly, in you JS application, you need to create a random subdomain (Math.random) and use it in all your Ajax call.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top