Skip to content

Instantly share code, notes, and snippets.

@iamparnab
Created November 7, 2020 13:34
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 iamparnab/e9cf81cd905cbd41fe6ea8956bc6e1a4 to your computer and use it in GitHub Desktop.
Save iamparnab/e9cf81cd905cbd41fe6ea8956bc6e1a4 to your computer and use it in GitHub Desktop.
Connect to SSE endpoint from JavaScript
function connect(path) {
const event = new EventSource(BASE_URL + path);
event.onopen = () => console.log("Opened");
event.onmessage = function (ev) {
const news = JSON.parse(ev.data);
render("#data", news.done ? "No more updates available." : news.value);
if (news.done) {
event.close();
markEnd();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment