FetchStreamTransport
FetchStreamTransport is the production-ready transport that opens a real server-sent event connection using the browser's fetch API and reads a ReadableStream response body. It is the default transport you register with provideStreamResource in production builds.
When you interact with it directly
In most apps you will never import or inject FetchStreamTransport by name — you register it once in provideStreamResource and forget about it. The two cases where you reach for it explicitly are:
- Per-resource override — you want one resource to use a different transport than the global default while everything else stays on
FetchStreamTransport. - Outside the DI tree — you are constructing a resource in a context where global providers are not available and you need to supply the transport manually.
import { inject } from '@angular/core';
import { streamResource, FetchStreamTransport } from '@cacheplane/stream-resource';
// Override transport for a single resource
const events = streamResource<Event>({
url: () => '/api/events',
transport: inject(FetchStreamTransport),
});How it works
FetchStreamTransport makes a fetch call to the given URL and expects the server to respond with Content-Type: text/event-stream. It then reads the ReadableStream body line-by-line, parses SSE data: fields, and emits each parsed JSON value into the resource signal.
The transport handles:
- Backpressure — reads chunks at the pace the browser delivers them
- Cancellation — aborts the underlying
fetchwheninterrupt()is called or the resource is destroyed - Error propagation — network errors and non-2xx responses surface through
resource.error()
FetchStreamTransport implements the StreamTransport interface. You can
create custom transports (e.g. WebSocket-backed) by implementing the same
interface and providing them in place of this class.
What's Next
Learn how the SSE lifecycle maps to resource signals and how to handle reconnects.
Server configuration for SSE: headers, timeouts, and edge runtime considerations.
The test-time counterpart — push values synchronously without a real server.