You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/sse/README.md
+101-7Lines changed: 101 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Primitives for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web
12
12
13
13
-[`makeSSE`](#makesse) — Base non-reactive primitive. Creates an `EventSource` and returns a cleanup function. No Solid lifecycle.
14
14
-[`createSSE`](#createsse) — Reactive primitive. Accepts a reactive URL, integrates with Solid's owner lifecycle, and returns signals for `data`, `error`, and `readyState`.
15
-
-[`makeSSEWorker`](./WORKERS.md) — Runs the SSE connection inside a Web Worker or SharedWorker. See [WORKERS.md](./WORKERS.md).
15
+
-[`makeSSEWorker`](#running-sse-in-a-worker) — Runs the SSE connection inside a Web Worker or SharedWorker.
For high-frequency streams or performance-sensitive apps you can offload the `EventSource` connection to a Web Worker, keeping network I/O off the main thread. The reactive API (`data`, `readyState`, `reconnect`, …) is identical — only the transport moves.
218
-
219
-
See [WORKERS.md](./WORKERS.md) for setup instructions, SharedWorker usage, and the full type reference.
220
-
221
215
## Built-in transformers
222
216
223
217
Ready-made `transform` functions for the most common SSE data formats. Pass one as the `transform` option to `createSSE`:
`@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.
324
+
325
+
### When to use this
326
+
327
+
-**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.
328
+
-**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).
329
+
330
+
For typical usage — a handful of events per second — the standard `createSSE` is simpler and sufficient.
331
+
332
+
### Setup
333
+
334
+
The adapter is in a separate subpath so it adds zero bytes to the main bundle when not used.
sw.port.start(); // required to activate a MessagePort
382
+
383
+
const { data } =createSSE("https://api.example.com/events", {
384
+
source: makeSSEWorker(sw.port),
385
+
});
386
+
```
387
+
388
+
`makeSSEWorker` accepts anything that satisfies `SSEWorkerTarget` — both `Worker` and `MessagePort` do.
389
+
390
+
### How it works
391
+
392
+
`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:
393
+
394
+
1. Creates a `WorkerEventSource` — an `EventTarget` that posts a `connect` message to the Worker and re-dispatches `open` / `message` / `error` events received back from it.
395
+
2. The Worker script (`worker-handler`) receives the `connect` message, creates a real `EventSource` there, and posts events back via `postMessage`.
396
+
3.`createSSE`'s reactive machinery — signals, reconnect timer, URL tracking, `onCleanup` — runs on the main thread as normal; it just talks to a `WorkerEventSource` instead of a real `EventSource`.
397
+
398
+
### Type reference
399
+
400
+
```ts
401
+
// @solid-primitives/sse/worker
402
+
403
+
function makeSSEWorker(target:SSEWorkerTarget):SSESourceFn;
404
+
405
+
/** Accepted by makeSSEWorker — satisfied by both Worker and SharedWorker.port */
0 commit comments