Created
January 18, 2015 16:12
-
-
Save mhamrah/354181aa8097cc5f3dc3 to your computer and use it in GitHub Desktop.
simple sse script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(!!window.EventSource) { | |
var source = new EventSource('/messages'); | |
var el = document.querySelector('#sse'); | |
source.addEventListener('published', function(e) { | |
console.log('published: ' + e.data); | |
var p = document.createElement('p'); | |
p.textContent = e.data; | |
el.appendChild(p); | |
}, false); | |
source.addEventListener('open', function(e) { | |
console.log('opened sse connection.'); | |
}, false); | |
source.addEventListener('error', function(e) { | |
console.log('sse error: ' + e); | |
}, false); | |
} else { | |
alert('sorry, you don\'t have SSE support!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment