Skip to content

SSE (Server-Sent Events)

If a route returns an iterable or async iterable, Oweb streams it as SSE.

import { setTimeout as delay } from 'node:timers/promises';
import { Route } from 'owebjs';
export default class SseRoute extends Route {
async *handle(req, res) {
if (req.query?.deny === '1') {
return res.status(401).send({ code: 'error.unauthorized' });
}
while (!req.signal.aborted) {
yield { time: Date.now() };
await delay(1000, undefined, { signal: req.signal });
}
}
}

req.signal is aborted when the client connection closes, so long-running SSE routes can stop loops and cancel abortable work cleanly.

This is useful for live feeds, progress updates, and long-running operations.