1

I'm working on an app which uses the window.EventSource to stream data's on to a table view. Since there is a restriction on the max-connections-per-server limit as 6, i'm trying to have one EventSource object at a given point of time, so fundamentally i need to close the first eventsource object on the second one's onopen method and then the second one's onmessage event will stream the data to the table view. Consider the below code runs on an onclick event,

if(firstEventSource) {
  firstEventSource.close();
}
var firstEventSource = new window.EventSource(endPoint);
firstEventSource.onmessage = function (evt) {
  //... code goes here
} 

The above code works but as i mentioned rather than closing the firstEventSource directly, i need to close it by making sure that the second Event source has opened it's connection.

Sai
  • 1,790
  • 5
  • 29
  • 51
  • max-connections-per-server is the maximum number of connections that the browser can make to the server. I don't see in your question how you are getting there? Why can't you just use one `EventSource`? – baynezy May 17 '16 at 12:58
  • @baynezy when i say max connections it is the maximum number of active connections. As i mentioned in the query i have that function inside an onclick event handler and then if i'm gonna create new window.EventSource object for each click then it's not allowing me to go more than 6. The "endPoint" here will be streaming the data continuously from the server. – Sai May 17 '16 at 15:27

1 Answers1

0

Don't use multiple EventSource. Use one and use named events instead (web archive) http://cjihrig.com/blog/the-server-side-of-server-sent-events/

This way create one EventSource and then make an API call to your server onclick changing the configuration of the single EventSource.

icc97
  • 11,395
  • 8
  • 76
  • 90
baynezy
  • 6,493
  • 10
  • 48
  • 73
  • thanks for sharing the URL. In my scenario i have this endpoint where i need to append a query to it from the text box, something like this, var firstEventSource = new window.EventSource(endPoint + '?lastEventId=0&s=' + $scope.values); where $scope.values is an array... so just wondering in this case if i'm going to use one EventSource and then if i have to change it's config (in this case the endpoint). how can i do that... any examples would be of great help. – Sai May 17 '16 at 16:52
  • So what I mean is that you have one EventSource, but you make AJAX calls to your server, these can include the query parameters you mentioned. Use that to change the behaviour of what the existing EventSource does from that point. There is no value in constantly destroying and creating connections. – baynezy May 18 '16 at 08:50