Stream using Response Object

Instead of generators you can return StreamResponse object in order to get more control over your stream code.

StreamService.ts
StreamResponseObjectController.ts
StreamExample.tsx
1import type { StreamResponse } from 'vovk';
2
3export type Token = { message: string };
4
5export default class StreamService {
6 static async streamTokens(resp: StreamResponse<Token>) {
7 const tokens: Token[] = [
8 { message: 'Hello,' },
9 { message: ' World' },
10 { message: ' from' },
11 { message: ' Stream' },
12 { message: '!' },
13 ];
14
15 for (const token of tokens) {
16 resp.send(token);
17 await new Promise((resolve) => setTimeout(resolve, 300));
18 }
19
20 await resp.close();
21 }
22}
1import type { StreamResponse } from 'vovk';
2
3export type Token = { message: string };
4
5export default class StreamService {
6 static async streamTokens(resp: StreamResponse<Token>) {
7 const tokens: Token[] = [
8 { message: 'Hello,' },
9 { message: ' World' },
10 { message: ' from' },
11 { message: ' Stream' },
12 { message: '!' },
13 ];
14
15 for (const token of tokens) {
16 resp.send(token);
17 await new Promise((resolve) => setTimeout(resolve, 300));
18 }
19
20 await resp.close();
21 }
22}