Skip to content

Instantly share code, notes, and snippets.

@loganhasson
Created July 1, 2014 22:15
Show Gist options
  • Save loganhasson/87b13dd255f953471320 to your computer and use it in GitHub Desktop.
Save loganhasson/87b13dd255f953471320 to your computer and use it in GitHub Desktop.
boilerplate js for actioncontroller::live
$(function(){
function initialize() {
var source = new EventSource('/stream?name=Logan');
source.addEventListener('message', function(e){
console.log("Received "+e.data);
updateMessagesList(e.data);
}, false);
source.addEventListener('open', function(e){
console.log('Connection was opened.');
}, false);
source.addEventListener('error', function(e){
if (e.readyState == EventSource.CLOSED) {
console.log("Connection was closed.");
} else {
console.log("Something else happened.");
}
}, false);
};
function updateMessagesList(event){
var message = $('<li>').text(event);
$('#messages').prepend(message);
};
if ($('#messages').size() > 0) {
initialize();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment