KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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, bu
Tags (comma-separated)
Save Edits
Cancel