Skip to content

Instantly share code, notes, and snippets.

@iamparnab
Last active November 7, 2020 13:04
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/4a37beb493d4caf80e48fb27d8af134d to your computer and use it in GitHub Desktop.
Save iamparnab/4a37beb493d4caf80e48fb27d8af134d to your computer and use it in GitHub Desktop.
SSE handler
function sseHandler(res) {
const mySubscription = ChannelFactNews();
res.writeHead(200, {
"Content-Type": "text/event-stream",
});
const timer = setInterval(() => {
const nextValue = mySubscription.next();
res.write(
`data: {"done":${nextValue.done}, "value": "${nextValue.value}"}\n\n`
);
if (nextValue.done) {
clearInterval(timer);
}
}, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment