-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.ts
38 lines (30 loc) · 808 Bytes
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import path from "node:path";
import {Readable} from "node:stream";
import {createSession} from "better-sse";
import express from "express";
const app = express();
app.use(express.static(path.resolve(__dirname, "./public")));
app.get("/sse", async (req, res) => {
/**
* Create a session instance.
*/
const session = await createSession(req, res);
/**
* Create a sample readable stream.
*/
const stream = Readable.from([1, 2, 3]);
/**
* Pipe the stream contents to the client.
*/
const done = await session.stream(stream);
/**
* Push a final 'stream' event with the 'done' property.
*/
session.push({done}, "stream");
});
const PORT = process.env.PORT ?? 8080;
app.listen(PORT, () => {
console.log(
`Server listening. Open http://localhost:${PORT} in your browser.`
);
});