0

I'm trying to use EventSource in javascript. But as soon as I use them, i'm having some trouble. First, when reloading the page, the EventSource can't reconnect. Second, using it prevents XmlHttpRequests to work properly. In the below code, if I comment the event_source related code, the xml request works fine. If I create the event source and add a listener, the xml request never succeed.

envent_source = new EventSource('event-source.php');
envent_source.addEventListener('update', function(event) {
  console.log("update event");
}, false);

var request = new XMLHttpRequest();
request.onreadystatechange = function() {
  if (this.readyState == this.DONE) {
    if (this.status == 200)
      console.log("received");
  }
}
request.open("GET", "document.xml");
request.send();

Did I missed something ? Do EventSource needs to be used with caution ?

Valentin Perrelle
  • 1,303
  • 1
  • 10
  • 17
  • I don't see any problem in the above code. I guess your HTTP server is not replying to GET document.xml request. Make sure the server handles GET requests for event-source.php and document.xml separately. – vinayr Sep 24 '12 at 08:03
  • How could it not handle them separately ? – Valentin Perrelle Sep 24 '12 at 08:35
  • I found the answer to this last question in another topic [link](http://stackoverflow.com/questions/3506574/how-do-i-configure-apache2-to-allow-multiple-simultaneous-connections-from-same) : php sessions prevent simultaneous exections of script of requests from same client. Thank you vinayr. – Valentin Perrelle Sep 24 '12 at 22:47

1 Answers1

0

As pointed out by vinyar, the problem isn't client side but server side. In my case, the server uses php sessions which ensure mutual exclusive executions of requests from the same client. Thus the GET requests are put in a waiting state until the event source script is terminated.

More information there : Simultaneous Requests to PHP Script

Community
  • 1
  • 1
Valentin Perrelle
  • 1,303
  • 1
  • 10
  • 17