Last active
November 7, 2020 13:04
-
-
Save iamparnab/4a37beb493d4caf80e48fb27d8af134d to your computer and use it in GitHub Desktop.
SSE handler
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
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