-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (34 loc) · 1.11 KB
/
index.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
39
40
41
42
import {type FastifyPluginAsync} from 'fastify';
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const test: FastifyPluginAsync = async (fastify, options): Promise<void> => {
fastify.get('/async-iterator', async (request, reply) => {
reply.sse((async function * source () {
for (let i = 0; i < 10; i++) {
await sleep(100);
yield {id: String(i), data: "Some message"};
}
})());
});
fastify.get('/single-events', async (request, reply) => {
for (let i = 0; i < 10; i++) {
await sleep(100);
reply.sse({id: String(i), data: "Some message"});
}
reply.sseContext.source.end();
request.socket.on("close", () => {
console.log("connection closed");
});
});
fastify.get('/events', async (request, reply) => {
for (let i = 0; i < 10; i++) {
await sleep(100);
reply.sse({ data: "Some message" });
}
// reply.sse((async function * source (){})());
reply.sseContext.source.end();
request.socket.on("close", () => {
console.log("connection closed");
});
});
};
export default test;