Skip to content

Server-Sent Events ​

A route that streams three events with createEventStream and closes the stream.

ts
import { App, createEventStream, defineCoreHandler } from 'routup';

export const app = new App();

app.get('/events', defineCoreHandler((event) => {
    const stream = createEventStream(event);

    stream.write({
        id: '1',
        event: 'tick',
        data: 'one',
    });
    stream.write({
        id: '2',
        event: 'tick',
        data: 'two',
    });
    stream.write({
        id: '3',
        event: 'tick',
        data: 'three',
    });
    stream.end();

    return stream.response;
}));

Try It ​

Install routup, serve the app from a second file and start it with your runtime as on the Quickstart:

typescript
import { serve } from 'routup';
import { app } from './app.ts';

serve(app, { port: 3000 });
bash
curl -N http://localhost:3000/events
# id: 1
# event: tick
# data: one
#
# id: 2
# event: tick
# data: two
#
# id: 3
# event: tick
# data: three
#

How It Works ​

  • The stream's response has content-type: text/event-stream, cache-control that forbids caching, x-accel-buffering: no and connection: keep-alive, plus the status and headers set on event.response.
  • write() serializes a message as id:, event:, retry: and one data: line per line of data, followed by a blank line. A string argument is sent as data alone.
  • Messages written before the handler returns wait in the stream until the client reads them. end() closes the stream; after that, write() returns false.
  • See createEventStream for writing over time and keep-alive comments.