# Server-Sent Event

> Server-Sent Event fields: id, event type, data, and retry interval.

- Name: `server-sent-event`
- Categories: api
- Detail page: https://open-types.dev/types/server-sent-event

## Install

```bash
# Types only
npx shadcn add @open-types/server-sent-event

# Types + Zod v4 validators
npx shadcn add @open-types/server-sent-event-zod

# Types + real-world examples
npx shadcn add @open-types/server-sent-event-examples
```

## Types

```typescript
export interface ServerSentEvent {
  id?: string;
  event?: string;
  data: string;
  retry?: number;
}
```

## Zod validator

```typescript
import * as z from "zod";
import type { ServerSentEvent } from "../types/server-sent-event";

export const serverSentEventSchema = z.object({
  id: z.string().optional(),
  event: z.string().optional(),
  data: z.string(),
  retry: z.number().int().min(0, { error: "Retry must be greater than or equal to 0" }).optional(),
}) satisfies z.ZodType<ServerSentEvent>;

export type { ServerSentEvent } from "../types/server-sent-event";
```

## Examples

```typescript
// Source: https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
// Captured: 2026-05
import type { ServerSentEvent } from "../types/server-sent-event";

export const serverSentEventExamples = {
  dataOnly: { data: "ping" } satisfies ServerSentEvent,
  named: { event: "userJoined", data: '{"userId":"user_321"}' } satisfies ServerSentEvent,
  withId: {
    id: "msg-101",
    event: "message",
    data: "Hello, world!",
  } satisfies ServerSentEvent,
  withRetry: {
    event: "reconnect",
    data: "",
    retry: 5000,
  } satisfies ServerSentEvent,
  multilineData: {
    id: "msg-102",
    event: "message",
    data: "Line one\nLine two\nLine three",
  } satisfies ServerSentEvent,
  fullFields: {
    id: "msg-103",
    event: "progress",
    data: '{"percent":42}',
    retry: 10000,
  } satisfies ServerSentEvent,
} as const;
```
