🍡 mochi

SSR framework for Svelte 5 + Bun with islands-based selective hydration

On this page

Server-Sent Events

Register an SSE stream with Mochi.sse(handler). The handler receives a MochiSseStream and the underlying Request, and runs once per client connection.

// file: src/index.ts
import { Mochi } from 'mochi-framework';

await Mochi.serve({
  routes: {
    '/sse/time': Mochi.sse((stream) => {
      stream.send(new Date().toISOString());
      const interval = setInterval(() => {
        stream.send(new Date().toISOString());
      }, 1000);
      stream.onClose(() => clearInterval(interval));
    }),
  },
});

stream.send(data, options?)

Push one SSE frame to the client. data is a string. options accepts event (named event type) and id (last-event-id).

stream.send(JSON.stringify({ ok: true }), { event: 'tick', id: '42' });

stream.close()

End the stream from the server. The connection terminates and every registered onClose callback fires.

stream.onClose(callback)

Register cleanup that runs when the stream ends — whether the server called close() or the client disconnected. Use it to clear timers, unsubscribe from event buses, and release per-connection state.

Events

Mochi.sse emits sse:open, sse:message, and sse:close on mochiEvents. consoleLogger() prints them by default.

See it in action

Live demos showing key concepts from this page