Binding event listeners to EventSource
suggest changeYou can bind listeners to an EventSource (a browser API) and listen on named event channels with addEventListener.
EventSource.addEventListener(name: String, callback: Function, [options])
name: The name related to the name of the channel the server is emitting events to.
callback: The callback function runs every time an event bound to the channel is emitted, the function provides the event as an argument.
options: Options that characterize the behavior of the event listener.
The following example listens for a heartbeat stream. The server sends events on the heartbeat channel, and this listener runs for each one:
var eventSource = new EventSource("api/heartbeat");
// ...
eventSource.addEventListener("heartbeat", function(event) {
var status = event.data;
if (status=='OK') {
// do something
}
});
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents