MCPcopy Index your code
hub / github.com/triggerdotdev/trigger.dev / sse

Function sse

apps/webapp/app/utils/sse.server.ts:20–84  ·  view source on GitHub ↗
({ request, pingInterval = 1000, updateInterval = 348, run }: SseProps)

Source from the content-addressed store, hash-verified

18};
19
20export function sse({ request, pingInterval = 1000, updateInterval = 348, run }: SseProps) {
21 if (env.DISABLE_SSE === "1" || env.DISABLE_SSE === "true") {
22 return new Response("SSE disabled", { status: 200 });
23 }
24
25 let pinger: NodeJS.Timer | undefined = undefined;
26 let updater: NodeJS.Timer | undefined = undefined;
27 let timeout: NodeJS.Timeout | undefined = undefined;
28
29 const abort = () => {
30 clearInterval(pinger);
31 clearInterval(updater);
32 clearTimeout(timeout);
33 };
34
35 return eventStream(request.signal, (send, close) => {
36 const safeSend = (args: { event?: string; data: string }) => {
37 try {
38 send(args);
39 } catch (error) {
40 if (error instanceof Error) {
41 if (error.name !== "TypeError") {
42 logger.debug("Error sending SSE, aborting", {
43 error: {
44 name: error.name,
45 message: error.message,
46 stack: error.stack,
47 },
48 args,
49 });
50 }
51 } else {
52 logger.debug("Unknown error sending SSE, aborting", {
53 error,
54 args,
55 });
56 }
57
58 close();
59 }
60 };
61
62 pinger = setInterval(() => {
63 if (request.signal.aborted) {
64 return abort();
65 }
66
67 safeSend({ event: "ping", data: new Date().toISOString() });
68 }, pingInterval);
69
70 updater = setInterval(() => {
71 if (request.signal.aborted) {
72 return abort();
73 }
74
75 run(safeSend, abort);
76 }, updateInterval);
77

Callers 5

loaderFunction · 0.90
loaderFunction · 0.90
loaderFunction · 0.90
callMethod · 0.90
callMethod · 0.90

Calls 3

abortFunction · 0.85
safeSendFunction · 0.85
runFunction · 0.50

Tested by

no test coverage detected