Solid Primitives SSE

# @solid-primitives/sse [![size](https://img.shields.io/badge/size-1.63_kB-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/sse) [![version](https://img.shields.io/npm/v/@solid-primitives/sse?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/sse) [![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-2.json)](https://github.com/solidjs-community/solid-primitives#contribution-process) [![tested with vitest](https://img.shields.io/badge/tested_with-vitest-6E9F18?style=for-the-badge&logo=vitest)](https://vitest.dev) Primitives for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) using the browser's built-in `EventSource` API. Designed for Solid 2.0's async reactivity model. - [`makeSSE`](#makesse) — Base non-reactive primitive. Creates an `EventSource` and returns a cleanup function. No Solid lifecycle. - [`createSSE`](#createsse) — Reactive primitive. Accepts a reactive URL, integrates with Solid's owner lifecycle, and returns signals for `data` and `readyState`. - [`makeSSEAsyncIterable`](#makesseasynciterable) — Wraps an SSE endpoint as an `AsyncIterable`. Non-reactive foundation. - [`createSSEStream`](#createssestream) — Minimal reactive stream: just a `data` accessor backed by an async iterable. - [`makeSSEWorker`](#running-sse-in-a-worker) — Runs the SSE connection inside a Web Worker or SharedWorker. - [Built-in transformers](#built-in-transformers) — `json`, `ndjson`, `lines`, `number`, `safe`, `pipe`. ## Installation ```bash npm install @solid-primitives/sse # or yarn add @solid-primitives/sse # or pnpm add @solid-primitives/sse ``` ## `makeSSE` Creates a raw `EventSource` connection without any Solid lifecycle management. Event handlers are attached immediately. You are responsible for calling the returned cleanup function. This is the foundation primitive — `createSSE` uses it internally. ```tsx repl import { render } from "@solidjs/web"; import { createSignal, For, onCleanup } from "solid-js"; import { makeSSE } from "@solid-primitives/sse"; function Demo() { const [messages, setMessages] = createSignal([]); const [status, setStatus] = createSignal("disconnected"); let cleanup: VoidFunction | undefined; const disconnect = () => { cleanup?.(); cleanup = undefined; setStatus("disconnected"); }; const connect = () => { disconnect(); setStatus("connecting"); [, cleanup] = makeSSE("https://sse.dev/test", { onOpen: () => setStatus("open"), onMessage: e => setMessages(m => [e.data, ...m].slice(0, 5)), onError: () => setStatus("error"), }); }; onCleanup(disconnect); return ( <>

status: {status()}

); } render(() => , document.getElementById("app")!); ``` ```ts import { makeSSE } from "@solid-primitives/sse"; const [source, cleanup] = makeSSE("https://api.example.com/events", { onOpen: () => console.log("Connected"), onMessage: e => console.log("Message:", e.data), onError: e => console.error("Error:", e), events: { // Named SSE event types (server sends `event: update`) update: e => console.log("Update:", e.data), }, }); // When done: cleanup(); ``` ### Definition ```ts function makeSSE( url: string | URL, options?: SSEOptions, ): [source: EventSource, cleanup: VoidFunction]; type SSEOptions = { withCredentials?: boolean; onOpen?: (event: Event) => void; onMessage?: (event: MessageEvent) => void; onError?: (event: Event) => void; events?: Record void>; }; ``` ## `createSSE` Reactive SSE primitive. Connects on creation, closes when the owner is disposed, and reacts to URL changes. ```tsx repl import { render } from "@solidjs/web"; import { Errored, Loading } from "solid-js"; import { createSSE, SSEReadyState } from "@solid-primitives/sse"; const STATES = Object.keys(SSEReadyState); // ["CONNECTING", "OPEN", "CLOSED"] function Demo() { const { data, readyState, close, reconnect } = createSSE("https://sse.dev/test"); // After Reconnect, the new state shows together with the next message (held as one transition) return ( <>

readyState: {STATES[readyState()]}

Connection failed

}> Waiting for first message…

}>
{data()}
); } render(() => , document.getElementById("app")!); ``` ```ts import { createSSE, SSEReadyState } from "@solid-primitives/sse"; const { data, readyState, close, reconnect } = createSSE<{ message: string }>( "https://api.example.com/events", { transform: JSON.parse, reconnect: { retries: 3, delay: 2000 }, }, ); ``` ### Loading and error boundaries `data()` integrates with Solid 2.0's async reactivity: - **``** — shows fallback while `data()` is pending (before the first message arrives). - **``** — catches terminal errors (connection CLOSED with no retries left) thrown through `data()`. ```tsx import { Loading, Errored } from "solid-js"; import { createSSE } from "@solid-primitives/sse"; const { data, close, reconnect } = createSSE<{ message: string }>( "https://api.example.com/events", { transform: JSON.parse }, ); return (

Connection failed

}> Connecting…

}>

Latest: {data().message}

); ``` Non-terminal errors (while the browser is reconnecting automatically) are surfaced via the `onError` callback only — they don't interrupt the reactive graph. ### Stale-while-revalidating with `isPending` After the first message has arrived, subsequent reconnects (URL change, `reconnect()` call) put the connection back into a pending state. Use `isPending` from Solid to show a subtle "refreshing" indicator without replacing the whole subtree: ```tsx import { isPending } from "solid-js"; import { createSSE } from "@solid-primitives/sse"; const { data } = createSSE<{ msg: string }>(url, { transform: JSON.parse }); return ( <> data())}>

Refreshing…

Connecting…

}>

{data().msg}

); ``` > **Note:** `isPending` is `false` during the initial `` fallback (no stale value yet). It becomes `true` only when a stale value exists and new data is pending — i.e., after a URL change or reconnect. ### Reactive URL with `` When the URL is a signal accessor, the connection is replaced whenever the URL changes. Use ``'s `on` prop to re-show the fallback on each URL change: ```tsx const [userId, setUserId] = createSignal("user-1"); const { data } = createSSE( () => `https://api.example.com/notifications/${userId()}`, { transform: JSON.parse }, ); return ( // on={userId()} re-shows the fallback each time userId changes while pending Connecting…

}>

{data().message}

); ``` Without `on`, `` keeps showing stale content during revalidation. With `on`, it re-shows the fallback whenever the key changes and a new connection is establishing. ### Options | Option | Type | Default | Description | | ----------------- | ------------------------------------------- | ----------- | ---------------------------------------- | | `withCredentials` | `boolean` | `false` | Send credentials with the request | | `onOpen` | `(e: Event) => void` | — | Called when the connection opens | | `onMessage` | `(e: MessageEvent) => void` | — | Called on each unnamed `message` event | | `onError` | `(e: Event) => void` | — | Called on error (terminal and transient) | | `events` | `Record void>` | — | Handlers for named SSE event types | | `initialValue` | `T` | `undefined` | Initial value of the `data` signal | | `transform` | `(raw: string) => T` | identity | Parse raw string data, e.g. `JSON.parse` | | `reconnect` | `boolean \| SSEReconnectOptions` | `false` | App-level reconnect on terminal errors | **`SSEReconnectOptions`:** | Option | Type | Default | Description | | --------- | -------- | ---------- | ----------------------------- | | `retries` | `number` | `Infinity` | Max reconnect attempts | | `delay` | `number` | `3000` | Milliseconds between attempts | ### Return value | Property | Type | Description | | ------------ | ---------------------------------------- | ------------------------------------------------------------------------------------------- | | `source` | `Accessor` | Underlying source instance; `undefined` on SSR | | `data` | `Accessor` | Latest message data; throws `NotReadyError` until first message, terminal errors thereafter | | `readyState` | `Accessor` | `SSEReadyState.CONNECTING` / `.OPEN` / `.CLOSED` | | `close` | `VoidFunction` | Close the connection | | `reconnect` | `VoidFunction` | Force-close and reopen; resets `data` to pending | ### Initial value Provide `initialValue` to skip the pending state entirely — `data()` returns it immediately with no `` fallback needed: ```ts const { data } = createSSE(url, { initialValue: [] as string[] }); // data() === [] immediately, no Loading needed ``` ### `SSEReadyState` Named constants for the connection state, exported as a plain object so they are tree-shakeable and work with every bundler: ```ts import { SSEReadyState } from "@solid-primitives/sse"; SSEReadyState.CONNECTING; // 0 SSEReadyState.OPEN; // 1 SSEReadyState.CLOSED; // 2 ``` ### A note on reconnection `EventSource` has native browser-level reconnection built in. For transient network drops the browser automatically retries. The `reconnect` option in `createSSE` is for _application-level_ reconnection — it fires only when `readyState` becomes `SSEReadyState.CLOSED`, meaning the browser has given up entirely. You generally do not need `reconnect: true` for normal usage. ## `makeSSEAsyncIterable` Wraps an SSE endpoint as a standard `AsyncIterable`. Each SSE message becomes one yielded value; terminal errors (connection CLOSED) are thrown by the iterator. Cleanup runs automatically when the iterator is abandoned via `return()`. Use this as a non-reactive building block: integrate it with a `for await…of` loop, pass it to your own `createMemo`, or compose it with other async utilities. ```ts import { makeSSEAsyncIterable } from "@solid-primitives/sse"; const iterable = makeSSEAsyncIterable("https://api.example.com/events"); for await (const msg of iterable) { console.log(msg); } ``` ### Definition ```ts function makeSSEAsyncIterable( url: string | URL, options?: CreateSSEStreamOptions, ): AsyncIterable; type CreateSSEStreamOptions = { withCredentials?: boolean; onOpen?: (event: Event) => void; onError?: (event: Event) => void; transform?: (raw: string) => T; events?: Record void>; source?: SSESourceFn; }; ``` ## `createSSEStream` A minimal reactive alternative to `createSSE` that returns only a `data` accessor. Internally it drives an `AsyncIterable` produced by `makeSSEAsyncIterable`, giving the same `` / `` integration with less API surface. Use this when you only need the stream values and don't need access to `source`, `readyState`, `close`, or `reconnect`. ```ts import { createSSEStream } from "@solid-primitives/sse"; const data = createSSEStream<{ msg: string }>(url, { transform: JSON.parse }); return (

Connection failed

}> Connecting…

}>

{data().msg}

); ``` Reactive URL is supported — the stream reconnects automatically when the URL signal changes: ```ts const [userId, setUserId] = createSignal("user-1"); const data = createSSEStream( () => `https://api.example.com/notifications/${userId()}`, { transform: JSON.parse }, ); ``` ### Definition ```ts function createSSEStream( url: MaybeAccessor, options?: CreateSSEStreamOptions, ): Accessor; ``` ## Integration with `@solid-primitives/event-bus` Because `bus.emit` matches the `(event: MessageEvent) => void` shape of `onMessage`, you can wire them directly: ```ts import { createSSE } from "@solid-primitives/sse"; import { createEventBus } from "@solid-primitives/event-bus"; const bus = createEventBus(); createSSE("https://api.example.com/events", { onMessage: e => bus.emit(e.data), }); bus.listen(msg => console.log("received:", msg)); ``` ### Multi-channel SSE with `createEventHub` For streams that use multiple named event types: ```ts import { createSSE } from "@solid-primitives/sse"; import { createEventBus, createEventHub } from "@solid-primitives/event-bus"; type OrderEvent = { id: string; total: number }; type InventoryEvent = { sku: string; qty: number }; const hub = createEventHub({ order: createEventBus(), inventory: createEventBus(), }); createSSE("https://api.example.com/stream", { events: { order: e => hub.emit("order", JSON.parse(e.data)), inventory: e => hub.emit("inventory", JSON.parse(e.data)), }, }); hub.on("order", event => console.log("New order:", event)); ``` ### Building a reactive message list ```ts import { createSSE } from "@solid-primitives/sse"; import { createStore } from "solid-js"; const [messages, setMessages] = createStore([]); createSSE("https://api.example.com/events", { onMessage: e => setMessages(msgs => [...msgs, e.data]), }); return {msg =>

{msg}

}
; ``` ## Built-in transformers Ready-made `transform` functions for the most common SSE data formats. Pass one as the `transform` option to `createSSE` or `createSSEStream`: ```ts import { createSSE, json } from "@solid-primitives/sse"; const { data } = createSSE<{ status: string }>(url, { transform: json }); ``` | Transformer | Description | | --------------------------------- | --------------------------------------------------------------- | | [`json`](#json) | Parse data as a single JSON value | | [`ndjson`](#ndjson) | Parse newline-delimited JSON into an array | | [`lines`](#lines) | Split data into a `string[]` by newline | | [`number`](#number) | Parse data as a number via `Number()` | | [`safe`](#safetransform-fallback) | Fault-tolerant wrapper — returns `fallback` instead of throwing | | [`pipe`](#pipea-b) | Compose two transforms into one | ### `json` Parse the message data as a single JSON value. Equivalent to `JSON.parse` but named for consistency with the other transformers. ```ts import { createSSE, json } from "@solid-primitives/sse"; const { data } = createSSE<{ status: string; ts: number }>(url, { transform: json }); // data() === { status: "ok", ts: 1718000000 } ``` ### `ndjson` Parse the message data as [newline-delimited JSON](https://ndjson.org/) (NDJSON / JSON Lines). Each non-empty line is parsed as a separate JSON value and the transformer returns an array. Use this when the server batches multiple objects into one SSE event: ``` data: {"id":1,"type":"tick"} data: {"id":2,"type":"tick"} ``` ```ts import { createSSE, ndjson } from "@solid-primitives/sse"; const { data } = createSSE(url, { transform: ndjson }); // data() === [{ id: 1, type: "tick" }, { id: 2, type: "tick" }] ``` ### `lines` Split the message data into individual lines, returning a `string[]`. Empty lines are filtered out. Useful for multi-line text events that are not JSON. ```ts import { createSSE, lines } from "@solid-primitives/sse"; const { data } = createSSE(url, { transform: lines }); // data() === ["line one", "line two"] ``` ### `number` Parse the message data as a number using `Number()` semantics. Handy for streams that emit counters, progress percentages, sensor readings, or prices. ```ts import { createSSE, number } from "@solid-primitives/sse"; const { data } = createSSE(url, { transform: number }); // data() === 42 ``` Note: follows `Number()` coercion — an empty string becomes `0` and non-numeric strings become `NaN`. ### `safe(transform, fallback?)` Wraps any transform in a `try/catch`. When the inner transform throws, `safe` returns `fallback` instead of propagating the error. This keeps the stream alive across malformed events. ```ts import { createSSE, json, number, safe } from "@solid-primitives/sse"; // Returns undefined on a bad event instead of throwing const { data } = createSSE(url, { transform: safe(json) }); // With an explicit fallback value const { data } = createSSE(url, { transform: safe(number, 0) }); ``` ### `pipe(a, b)` Composes two transforms into one: the output of `a` is passed as the input of `b`. Useful for building custom transforms from existing primitives without writing anonymous functions. ```ts import { createSSE, ndjson, json, safe, pipe } from "@solid-primitives/sse"; // Parse NDJSON then keep only "tick" rows type RawEvent = { type: string }; const { data } = createSSE(url, { transform: pipe(ndjson, rows => rows.filter(r => r.type === "tick")), }); // Safe JSON with a post-processing step const { data } = createSSE(url, { transform: pipe(safe(json<{ label: string }>), ev => ev?.label ?? ""), }); ``` ## Running SSE in a Worker `@solid-primitives/sse` ships a `makeSSEWorker` adapter that moves the `EventSource` connection into a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) or a [SharedWorker](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker). The reactive API you get back from `createSSE` is identical — `data`, `readyState`, `reconnect`, etc. work exactly as documented above. ### `makeSSEWorker` Tunnels `createSSE`'s connection through a Worker running the `worker-handler` script — pass it as the `source` option. ```tsx repl import { render } from "@solidjs/web"; import { createSignal, Errored, Loading, Show } from "solid-js"; import { createSSE } from "@solid-primitives/sse"; import { makeSSEWorker } from "@solid-primitives/sse/worker"; // Load the worker-handler entry in a module Worker // (a bundler would use `new URL(…, import.meta.url)` instead of a blob URL) const handler = `import "https://esm.sh/@solid-primitives/sse@next/worker-handler";`; const worker = new Worker(URL.createObjectURL(new Blob([handler], { type: "text/javascript" })), { type: "module", }); const [failed, setFailed] = createSignal(false); worker.onerror = () => setFailed(true); function Demo() { const { data } = createSSE("https://sse.dev/test", { source: makeSSEWorker(worker) }); return ( Worker failed to load

}> Connection failed

}> Waiting for the worker…

}>
{data()}
); } render(() => , document.getElementById("app")!); ``` ### When to use this - **High-frequency streams** — parsing and dispatching many events per second on the main thread can cause jank. Moving the connection to a Worker keeps that work off the UI thread. - **SharedWorker** — if multiple tabs in the same origin connect to the same SSE endpoint, a SharedWorker lets them share a single Worker process (though each tab still gets its own `EventSource` connection inside the worker). For typical usage — a handful of events per second — the standard `createSSE` is simpler and sufficient. ### Setup ```ts import { makeSSEWorker } from "@solid-primitives/sse/worker"; ``` You also need the companion handler script that runs inside the Worker: ```ts import "@solid-primitives/sse/worker-handler"; ``` To get the correct URL for the handler at runtime you have a few options depending on your setup: - **Bundler (Vite, Webpack, Rollup, etc.)** — use `new URL(…, import.meta.url)`. The bundler resolves the specifier to the output asset path at build time. See the [Vite static asset docs](https://vite.dev/guide/assets#importing-asset-as-url) for details; other bundlers work the same way. - **Import maps (no bundler)** — add an entry for `@solid-primitives/sse/worker-handler` pointing to the CDN or local path of the file, then use a plain string URL: `new Worker("/path/to/worker-handler.js", { type: "module" })`. - **Node / Deno / Bun with a file URL** — `new URL("./node_modules/@solid-primitives/sse/dist/worker-handler.js", import.meta.url)` works if you reference the built output directly. ### Dedicated Worker ```ts import { createSSE } from "@solid-primitives/sse"; import { makeSSEWorker } from "@solid-primitives/sse/worker"; const worker = new Worker(new URL("@solid-primitives/sse/worker-handler", import.meta.url), { type: "module", }); const { data, readyState, close, reconnect } = createSSE<{ msg: string }>( "https://api.example.com/events", { source: makeSSEWorker(worker), transform: JSON.parse, reconnect: { retries: 3, delay: 2000 }, }, ); ``` That's the only change compared to a standard `createSSE` call — pass `source: makeSSEWorker(worker)` and everything else stays the same. ### SharedWorker A SharedWorker is shared across all tabs on the same origin. Pass `sw.port` (a `MessagePort`) in place of the `Worker` instance: ```ts import { createSSE } from "@solid-primitives/sse"; import { makeSSEWorker } from "@solid-primitives/sse/worker"; const sw = new SharedWorker(new URL("@solid-primitives/sse/worker-handler", import.meta.url), { type: "module", }); sw.port.start(); // required to activate a MessagePort const { data } = createSSE("https://api.example.com/events", { source: makeSSEWorker(sw.port), }); ``` `makeSSEWorker` accepts anything that satisfies `SSEWorkerTarget` — both `Worker` and `MessagePort` do. ### How it works `makeSSEWorker(target)` returns an `SSESourceFn`, the same factory interface that `createSSE` uses internally. When `createSSE` opens a connection it calls this factory instead of the default `makeSSE`, which: 1. Creates a plain `EventTarget` with a `readyState` property and a `close()` method, satisfying the `SSESourceHandle` interface without needing a real `EventSource`. 2. Posts a `connect` message to the Worker. The Worker script (`worker-handler`) creates a real `EventSource` there and posts `open` / `message` / `error` events back via `postMessage`. 3. The message listener on the main thread forwards those events to `createSSE`'s callbacks and dispatches them on the `EventTarget` so any direct `addEventListener` calls also work. 4. `createSSE`'s reactive machinery — signals, reconnect timer, URL tracking, `onCleanup` — runs on the main thread as normal; it just receives events via `postMessage` instead of directly from a real `EventSource`. ### Type reference ```ts // @solid-primitives/sse/worker function makeSSEWorker(target: SSEWorkerTarget): SSESourceFn; /** Accepted by makeSSEWorker — satisfied by both Worker and SharedWorker.port */ type SSEWorkerTarget = { postMessage(data: SSEWorkerMessage): void; addEventListener(type: "message", listener: (e: MessageEvent) => void): void; removeEventListener(type: "message", listener: (e: MessageEvent) => void): void; }; /** Messages exchanged between the main thread and the Worker */ type SSEWorkerMessage = | { type: "connect"; id: string; url: string; withCredentials?: boolean; events?: string[] } | { type: "disconnect"; id: string } | { type: "open"; id: string } | { type: "message"; id: string; data: string; eventType: string } | { type: "error"; id: string; readyState: SSEReadyStateValue }; ``` ## Changelog See [CHANGELOG.md](./CHANGELOG.md).