Implementing Server-Sent Events with `effect-http`/`@effect/platform` in TypeScript

Any tips on how to implement something like this with
effect-http
/
@effect/platform
?
export async function handlePoke(
  req: Express.Request,
  res: Express.Response,
): Promise<void> {
  const channel = 'channel';

  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Content-Type', 'text/event-stream;charset=utf-8');
  res.setHeader('Cache-Control', 'no-cache, no-transform');
  res.setHeader('X-Accel-Buffering', 'no');

  res.write(`id: ${Date.now()}\n`);
  res.write(`data: hello\n\n`);

  const unlisten = () => {
    console.log(`Sending for channel ${channel}`);
    res.write(`id: ${Date.now()}\n`);
    res.write(`data: poke\n\n`);
  };

  setInterval(() => {
    res.write(`id: ${Date.now()}\n`);
    res.write(`data: beat\n\n`);
  }, 30 * 1000);

  res.on('close', () => {
    console.log('Closing connection');
    unlisten();
  });
}
Was this page helpful?