Skip to Content
JSONLines

JSON Lines example

Text streaming  with async generators .

Result

Code

JSONLinesController.ts
JSONLinesExample.tsx
1import { get, prefix, operation } from 'vovk';
2
3type Token = { message: string };
4
5@prefix('jsonlines')
6export default class JSONLinesController {
7 @operation({
8 summary: 'Stream tokens',
9 description: 'Stream tokens to the client',
10 })
11 @get('tokens', { cors: true })
12 static async *streamTokens() {
13 const tokens: Token[] = [
14 { message: 'Hello,' },
15 { message: ' World' },
16 { message: ' from' },
17 { message: ' Stream' },
18 { message: '!' },
19 ];
20
21 for (const token of tokens) {
22 yield token;
23 await new Promise((resolve) => setTimeout(resolve, 300));
24 }
25 }
26}
1import { get, prefix, operation } from 'vovk';
2
3type Token = { message: string };
4
5@prefix('jsonlines')
6export default class JSONLinesController {
7 @operation({
8 summary: 'Stream tokens',
9 description: 'Stream tokens to the client',
10 })
11 @get('tokens', { cors: true })
12 static async *streamTokens() {
13 const tokens: Token[] = [
14 { message: 'Hello,' },
15 { message: ' World' },
16 { message: ' from' },
17 { message: ' Stream' },
18 { message: '!' },
19 ];
20
21 for (const token of tokens) {
22 yield token;
23 await new Promise((resolve) => setTimeout(resolve, 300));
24 }
25 }
26}
Last updated on