Skip to content

Commit

Permalink
sse
Browse files Browse the repository at this point in the history
  • Loading branch information
hnasr committed Jul 13, 2020
1 parent cb0090d commit 865ce3c
Show file tree
Hide file tree
Showing 3 changed files with 418 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server-sent-events/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Client Code
let sse = new EventSource("http://localhost:8080/stream");
sse.onmessage = console.log
*/

const app = require("express")();

app.get("/", (req, res) => res.send("hello!"));

app.get("/stream", (req,res) => {

res.setHeader("Content-Type", "text/event-stream");
send(res);

})

let i = 0;
function send (res) {

res.write("data: " + `hello!${i++}\n\n`);


setTimeout(() => send(res), 1000);
}

app.listen(8080)
console.log("Listening on 8080")
Loading

0 comments on commit 865ce3c

Please sign in to comment.