### Implementing Server-Sent Events (SSE) with `@effect/platform` in TypeScript
I asked this before, but started using
but not sure how to define the API. E.g.
won't work obviously
@effect/platform@effect/platform instead of effect-httpeffect-http, and I'm trying to implement SSE. Got something like thisEffect.gen(function* () {
const setRetry = Stream.succeed("retry: 10000");
const keepAlive = Stream.schedule(Effect.succeed(":keep-alive"), Schedule.fixed(Duration.seconds(15)));
const encoder = new TextEncoder();
const initialEvent = Stream.succeed(`id: ${Date.now()}\ndata: hello`);
// Write the stream to the response
const stream = pipe(
setRetry,
Stream.merge(keepAlive),
Stream.merge(initialEvent),
Stream.map((_) => encoder.encode(`${_}\n\n`)),
);
yield* HttpApp.appendPreResponseHandler((_req, response) =>
Effect.orDie(
HttpServerResponse.setHeaders(response, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
"X-Accel-Buffering": "no",
Connection: "keep-alive",
"Access-Control-Allow-Origin": "*",
}),
),
);
return ???;
})Effect.gen(function* () {
const setRetry = Stream.succeed("retry: 10000");
const keepAlive = Stream.schedule(Effect.succeed(":keep-alive"), Schedule.fixed(Duration.seconds(15)));
const encoder = new TextEncoder();
const initialEvent = Stream.succeed(`id: ${Date.now()}\ndata: hello`);
// Write the stream to the response
const stream = pipe(
setRetry,
Stream.merge(keepAlive),
Stream.merge(initialEvent),
Stream.map((_) => encoder.encode(`${_}\n\n`)),
);
yield* HttpApp.appendPreResponseHandler((_req, response) =>
Effect.orDie(
HttpServerResponse.setHeaders(response, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
"X-Accel-Buffering": "no",
Connection: "keep-alive",
"Access-Control-Allow-Origin": "*",
}),
),
);
return ???;
})but not sure how to define the API. E.g.
class UsersApi extends HttpApiGroup.make("test").pipe(
HttpApiGroup.add(
HttpApiEndpoint.get("stream", "/test/stream").pipe(
HttpApiEndpoint.setSuccess(
Schema.String.pipe(
HttpApiSchema.withEncoding({
kind: "Text",
contentType: "text/event-stream"
})
)
)
)
)
) {}class UsersApi extends HttpApiGroup.make("test").pipe(
HttpApiGroup.add(
HttpApiEndpoint.get("stream", "/test/stream").pipe(
HttpApiEndpoint.setSuccess(
Schema.String.pipe(
HttpApiSchema.withEncoding({
kind: "Text",
contentType: "text/event-stream"
})
)
)
)
)
) {}won't work obviously
