alepha@docs:~/docs/reference/primitives$
cat $sse.md | pretty2 min read
#$sse
#Import
typescript
1import { $sse } from "alepha/server";
#Overview
Creates a Server-Sent Events (SSE) primitive for streaming typed events to clients.
SSE endpoints provide a unidirectional stream from server to client over HTTP,
with full type safety for event data. The handler receives emit() and close()
functions to control the stream.
Key Features
- Full TypeScript inference for event data types
- Automatic schema validation using Zod
- An explicit path on every endpoint, no convention to remember
- Direct invocation (
run()) returns anSseStreamasync iterable - HTTP requests (
fetch()) returns anSseFetchResponseasync iterable - Built-in
text/event-streamcontent-type handling
URL Generation
All $sse paths are automatically prefixed with /api.
ts
1$sse({ path: "/events" }) // POST /api/events2$sse({ path: "/feed/:id" }) // POST /api/feed/:id
The HTTP method is always POST. path is required, so renaming the
property never moves the URL.
#Options
| Option | Type | Required | Description |
|---|---|---|---|
name |
string |
No | Name of the SSE endpoint. |
group |
string |
No | Group SSE endpoints together. |
path |
string |
Yes | Pathname of the route, below the API prefix (/api by default) |
schema |
TConfig |
No | The config schema for the SSE endpoint. |
description |
string |
No | A short description of the endpoint |
disabled |
boolean |
No | Disable the SSE endpoint. |
handler |
SseHandler<TConfig> |
Yes | Main SSE handler |
#Examples
ts
1class NotificationController { 2 events = $sse({ 3 path: "/notifications/events", 4 schema: { 5 data: z.object({ 6 type: z.text(), 7 message: z.text(), 8 }), 9 },10 handler: async ({ emit, close }) => {11 emit({ type: "welcome", message: "Connected!" });12 // ... stream events ...13 close();14 },15 });16}