Created
April 14, 2017 01:20
-
-
Save NigelEarle/110adb1b95dee1c7631523b52484d8f8 to your computer and use it in GitHub Desktop.
EventSource
This file contains 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) { | |
const source = new EventSource('/api/bubble-sort'); | |
source.onmessage = (e) => { | |
const data = JSON.parse(e.data); | |
// data parsed | |
}; | |
source.onopen = () => { | |
console.log('connected!'); | |
}; | |
source.onerror = (e) => { | |
if (e.target.readyState === EventSource.CLOSED) { | |
console.log('closed'); | |
} else if (e.target.readyState === EventSource.CONNECTING) { | |
console.log('trying to connect'); | |
} | |
}; | |
} else { | |
console.log("Your browser doesn't support SSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment