Should there be only one EventSource object per app?
html, http, javascript, push, server-sent-events
Solution
I would highly recommend, IMHO, that you have one `EventSource` object per SSE-providing service, and then emit the messages using different types.
Ultimately, though, it depends on how similar the message types are. For example, if you have 5 different types of messages related to users, have a user `EventSource` and differentiate with event types.
If you have one event type about users, and another about sandwiches, I'd say keep them in different services, and thus `EventSource`s.
It's a good idea to think of breaking up EventSources the same way you would a restful service. If you wouldn't get two things from the same service with AJAX, you probably shouldn't get them from the same EventSource.
Problem
When using Server-Sent Events should the client establish multiple connections to receive different events it is interested in, or should there be a single connection and the client indicates what it is interested via a separate channel? IMO the latter seems more preferable although to some it might make the client code more complex. The spec supports named events (events that relate to a particular topic), which to me suggests that a Server-Sent Events connection should be used as single channel for all events. The following code illustrates the first scenario where a multiple Server-Sent Event connections are initiated: ``` var EventSource eventSource1 = new EventSource("events/topic1"); eventSource1.addEventListener('topic1', topic1Listener, false); var EventSource eventSource2 = new EventSource("events/topic2"); eventSource2.addEventListener('topic2', topic2Listener, false); ``` eventSource1 would receive "topic1" events and eventSource2 would receive "topic2" events. Whilst this is pretty straight forward it is also pretty inefficient with a hanging GET occurring for each topic you are interested in. The alternative is something like the following: ``` var EventSource eventSource3 = new EventSource("/events?id=1234") eventSource3.addEventListener('topic3', topic3Listener, false); eventSource3.addEventListener('topic4', topic4Listener, false); var subscription = new XMLHttpRequest(); subscription.open("PUT", "/events/topic3?id=1234", true); subscription.send(); ``` In this example a single EventSource would exist and interest in a particular event would be specified by a separate request with the Server-Sent Event connection and the registration being correlated by the id param. topic3Listener would receive "topic3" events and topic4Listener would not. Whilst requiring slightly more code the benefit is that only a single connection is made, but events can be still be identified and handled differently. There are a number examples on the web that show the use of named events, but it seems the event names (or topics) are known in advance so there is no need for a client to register interest with the server (example). Whilst I am yet to see an example showing multiple EventSource objects, I also haven't seen an example showing a client using a separate request to register interest in a particular topic, as I am doing above. My interpretation of the spec leads me to believe that indicating an interest in a certain topic (or event name) is entirely up to the developer and that it can be done statically with the client knowing the names of the events it is going to receive or dynamically with the client alerting the server that it is interested in receiving particular events. I would be pretty interested in hearing other people's thoughts on the topic. NB: I am usually a Java dev so please forgive my mediocre JS code.. :)