Skip to Content
Polling

Polling example

Demonstrates infinite polling. The client sends i parameter to the server, the server increments it and streams the result back to the client. After 10 iterations, the server ends the response and the client starts polling again. Open devtools to see the network requests.

Result

Poll ticker

0

Code

PollController.ts
PollExample.tsx
1import { get, prefix } from 'vovk';
2import { withZod } from 'vovk-zod';
3import { z } from 'zod/v4';
4
5@prefix('poll')
6export default class PollController {
7 @get()
8 static streamPollResponse = withZod({
9 query: z.object({
10 i: z.string(),
11 }),
12 iteration: z.object({
13 i: z.number(),
14 }),
15 async *handle(req) {
16 let i = parseInt(req.vovk.query().i);
17 let k = 0;
18 while (true) {
19 yield { i: i++ };
20 await new Promise((resolve) => setTimeout(resolve, 1000));
21 if (++k >= 10) {
22 break;
23 }
24 }
25 },
26 });
27}
1import { get, prefix } from 'vovk';
2import { withZod } from 'vovk-zod';
3import { z } from 'zod/v4';
4
5@prefix('poll')
6export default class PollController {
7 @get()
8 static streamPollResponse = withZod({
9 query: z.object({
10 i: z.string(),
11 }),
12 iteration: z.object({
13 i: z.number(),
14 }),
15 async *handle(req) {
16 let i = parseInt(req.vovk.query().i);
17 let k = 0;
18 while (true) {
19 yield { i: i++ };
20 await new Promise((resolve) => setTimeout(resolve, 1000));
21 if (++k >= 10) {
22 break;
23 }
24 }
25 },
26 });
27}
Last updated on