Skip to content

Instantly share code, notes, and snippets.

@mmcc
Created July 17, 2014 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmcc/c013809fac73396006b6 to your computer and use it in GitHub Desktop.
Save mmcc/c013809fac73396006b6 to your computer and use it in GitHub Desktop.
EventSource example
function initEventSource(route) {
var source = new EventSource(route);
source.onopen = connectionEstablished;
source.onmessage = messageReceived;
source.onerror = connectionError;
return source;
}
function connectionEstablished(event) {
console.log('Connection established');
}
function messageReceived(event) {
var msg = JSON.parse(event.data);
}
function connectionError(event) {
if (e.readyState === EventSource.CLOSED) {
console.log('Connection closed.');
} else {
console.log('EventSource error', event);
}
}
$(function() {
var testSource = initEventSource('/test');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment